Linux Basics: Bash built-in variables

Source: Internet
Author: User

Bash creates many built-in variables by default, which are described below.

1. BASH

Purpose: Displays the full path name of bash. Usually:/bin/bash or/usr/local/bin/bash.

Use case: Echo $BASH

2. Bash_version

Purpose: The version of Bash.

Use case: Echo $BASH

4 Execution Result: 4.1.2 (1)-release

3. FUNCNAME

Purpose: In the functional execution period, the content of this variable is the name of the function type.

Case:

function Show_name () {

Echo $FUNCNAME

echo "This is $ $, you are big, you're from $."

}

4 Execution Result: Displays the function name Show_name.

4. Histcmd

Purpose: After the current instruction is executed, it arranges the numbering in the history instruction (the historical command number assigned to the next instruction).

Use case: Echo $HISTCMD

4 Execution Result: 1051

5. Histcontrol

Purpose: Controls whether the instruction is stored in the history script file.

Usage: Histcontrol has 3 possible setpoint values:

Ignorespace: There are no characters at the beginning of the instruction, and it is not stored in the history script file.

Ignoredups: Consecutive repeated instructions, save only one.

Ignoreboth: Combines the functions of the former two.

6. Histfile

Purpose: Sets the path file name of the history script file. Usually in: Home directory/.bash_history

Use case: Echo $HISTFILE

4 Execution Result:/root/.bash_history

7. Histfilesize

Purpose: Sets the maximum number of rows for the history script file store instruction.

Use case: Echo $HISTFILESIZE

4 Execution Result: 1000

8. Histignore

Purpose: The instruction style is not stored in the history script file, separated by:.

Use case: Echo histgnore=ls:cd:type

9. HOME

Purpose: Sets the user's home directory location. Usually, the home directory of root is in/root, the home directory of the general user in the/home/account. The result of replacing ~ is the value of the home variable.

Ten. HOSTNAME

Purpose: Displays the host name.

Use case: Echo $HOSTNAME

4 execution results: www.contoso.com

HOSTTYPE.

Purpose: Displays the host form.

Use case: Echo $HOSTTYPE

4 Execution Result: x86_64

IFS

Purpose: Defines the field separator character. The default values are: space characters, tab characters, line breaks (newline).

Use Case 1

[email protected] lab]# cat ifs1.sh
#!/bin/bash
#

chk= "T1 t2 t3 T4"

For i in $CHK; Do
Echo $i
Done

4 Execution Results:

T1
T2
T3
T4

Because one of the contents of $ifs is a space character, the $chk can be split into 4 parts (called fields) and presented to the Echo $i respectively.

Use Case 2

PL is a line in the password file/etc/passwd and is now analyzed for each field in the line.

#!/bin/bash
#

Pl= "Root:x:0:0:root:/root:/bin/bash"

# Redefine IFS with ': ' As a delimiter

Ifs= ': '

# J for the first few columns

J=0

# to cycle through the fields

For i in $PL; Do
j=$ ((j+1))
echo the $j field is a $i
Done

4 Execution Results:

The 1th field is root
The 2nd field is X
The 3rd field is 0
The 4th field is 0
The 5th field is root
The 6th field is/root
The 7th field is/bin/bash

MACHTYPE.

Purpose: Describes the GNU format of the host form: cpu-company-System.

Use case: Echo $MACHTYPE

4 Execution Results:

X86_64-redhat-linux-gnu

MAIL

Purpose: Displays the current user's mail directory.

Use case: Echo $MAIL

4 Execution Results:

/var/spool/mail/root

MailCheck.

Purpose: Check the mail every time. Typically, the default is 60 seconds.

Use case: Echo $MAILCHECK

4 Execution Result: 60

. PATH

Purpose: The search path of the command.

Usage: If you want to add a search path yourself, the general practice is as follows:

Export path=/lab/tmp: $PATH

This allows you to add paths to the path/lab/tmp

PPID.

Purpose: The process number of the parent process.

Use case: Echo $PPID

RANDOM

Purpose: Random function.

The value of this variable, which randomly appears as an integer with a range of 0-32767. However, although it is random, it is not really random, because the random number is the same every time. To do this, before using the random variable, arbitrarily set a number to random, as the number of randomly seed, so that the random number will not be generated each time its order is the same.

Case:

random=$$ #使用bash Shell process number as a random number seed

Echo $RANDOM

REPLY.

Purpose: When select and read do not have a read variable set, the default variable name is reply.

shlvl.

Purpose: The number of sub-shell levels.

$1~ $n

Purpose: Position parameter.

Pass in a program or function parameter, the first parameter is $ $, the second parameter is $-a, and the other analogy.

Please note that if the position parameter n exceeds 9, it is represented by ${n}.

$ A represents the name of the executing program.

$*.

Purpose: Represents all positional parameters and is treated as a string.

For example: test.sh ABC 123 xyz, the content of $* is the string "ABC 123 XYZ"

[email protected]

Purpose: Represents all positional parameters, but [email protected] represents the serial of each positional parameter.

For example: test.sh ABC 123 xyz, the contents of [email protected] are 3 strings of "ABC", "123" and "XYZ".

$#.

Purpose: The number of positional parameters.

such as: test.sh ABC 123 xyz, there are 3 parameters, $ #的值为3.

25. $?

Purpose: The return value after the last command execution has ended. Typically 0 represents a successful execution, and a non-0 represents an error.

$$.

Purpose: The current bash Shell's process number.

$_.

Use:

A. When script executes, Bash's absolute path.

B. When the previous command was executed, the last positional parameter, such as the previous command, was: test.sh ABC 123 xyz, then the value of $_ is ' xyz '.

c. When checking a message, the value of $_ is the message file name.

This article is from the "Craft Life" blog, so be sure to keep this source http://allenh.blog.51cto.com/481430/1695810

Linux Basics: Bash built-in variables

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.