$FUNCNAME、$LINENO、$PWD
FUNCNAME
andLINENO
Variables are often used for script debugging
FUNCNAME
Represents the name of the current function, and the scope of action is only for use in the function, and there is no value outside the function
LINENO
Indicates the line number in which the variable appears in the current script
PWD
Represents the current directory, corresponding topwd
Order
Existing scriptsThe content is as follows
#!/bin/bash testa() { echo 'func='$FUNCNAME,$LINENO } testa echo 'lineno:'$LINENO echo 'xx:'$FUNCNAME echo 'curpath:'$PWD
implement./
The command output is as follows
[tt@ecs-centos-7 ~]$ ./
func=testa,6
lineno:12
xx:
curpath:/home/tt
existtesta
In the functionFUNCNAME
The variable value istesta
, that is, the function name, has no value outside the function
LINENO
Whether in or outside the function, variables represent the current line number
$$、$PPID
These two variables represent the current process ID and the parent process ID in turn
existingThe script, the content is as follows
sleep 20
The sleep statement is to allow the execution script process to temporarily exit and verify the output results in another terminal.
#!/bin/bash #set -u echo 'cur pid:'$$ echo 'parent pid:'$PPID sleep 20
Execute in the current terminal./
, the results are as follows
[tt@ecs-centos-7 ~]$ ./ cur pid:13095 parent pid:12982
ExecutionBefore the script process exits, open another terminal and execute
ps -o pid,ppid,time,cmd -p 12982,13095
The command, the result is as follows
[tt@ecs-centos-7 ~]$ ps -o pid,ppid,time,cmd -p 12982,13095 PID PPID TIME CMD 12982 12981 00:00:00 -bash 13095 12982 00:00:00 /bin/bash ./
From the above results, it can be seen that./
After the command,The $$ variable represents executionThe script's process ID 13095 , and 12982 is its parent process ID, that is,
PPID
The value of the variable, it is an instance of the current bash
$0,$1,$2...$n、$#
$#
Indicates the number of parameters passed into the script from the command line
$0,$1,$2...$n
is the parameter passed from the command line to the script$0
It's the name of the script itself$1
It's the first parameter$2
is the second parameter, and so on$n
It is the nth parameter
The tenth parameter and subsequent parameters must be enclosed in braces, for example: ${10}, ${11}, ${12} represents the tenth variable, the eleventh variable, and the twelfth variable in turn.
$*、$@
They all represent positional parameters, but there are some differences between them.
use$*
When adding double quotes, that is, the form of $*, then all the parameters at all positions will be processed as a word. If there is no double quotes, that is, the form of $*, then the parameters at each position will be processed as an independent word.
And for$@
, whether double quotes are added or not, the parameters at each position are processed as an independent word
existing, The content is as follows
#!/bin/bash cnt=1 echo 'test 1111' for var in "$*" do echo "arg$cnt="$var let "cnt+=1" done echo cnt=1 echo 'test 2222' for var in $* do echo "arg$cnt="$var let "cnt+=1" done echo cnt=1 echo 'test 3333' for var in "$@" do echo "arg$cnt="$var let "cnt+=1" done echo cnt=1 echo "test 4444" for var in $@ do echo "arg$cnt="$var let "cnt+=1" done
implement./ 1 2 3
, the results are as follows
[root@ecs-centos-7 ~]# ./ 1 2 3
test 1111
arg1=1 2 3test 2222
arg1=1
arg2=2
arg3=3test 3333
arg1=1
arg2=2
arg3=3test 4444
arg1=1
arg2=2
arg3=3
From the above results, we can see that for $*, all positional parameters will be regarded as a word after adding double quotes.
For $@, the results are the same as if you add double quotes.
So, only when using double quotes, $* and $@ will have a difference
$?
The exit status of a command, function or script is useful when judging the execution result of a command or the call result of a function.
existingand
Test scripts
script
#!/bin/bash test_func() { if [[ $1 -eq 10 ]]; then return 5 fi return 6 } if [ $# -ge 1 ]; then name="$1" shift 1 $name "$@" fi
script
#!/bin/bash sh test_func 3 echo 'exit code1:'$? sh test_func 10 echo 'exit code2:'$? test -f $PWD/ echo 'exit code3:'$? test -f $PWD/ echo 'exit code4:'$?
implement./
The command, the result is as follows
[root@ecs-centos-7 ~]# ./
exit code1:6
exit code2:5
exit code3:1
exit code4:0
scriptmiddle
test_func
Function function is: When the parameter is equal to 10, the exit status is 5, otherwise it is 6
sh test_func 3
The command will be calledIn the script
test_func
The function, the incoming parameter is 3, so the exit status is 6. Similarly, it can be seen thatsh test_func 10
The exit status of the command is 5
In Linux, the command execution is successful, the exit status is 0, and if it fails, it is non-0
test -f $PWD/
The command is to check whether the current directory existsFile, because the current directory does not exist
, so the command execution fails and the exit status is non-0
becauseExist in the current directory, so
test -f $PWD/
The command execution is successful, and the exit status is 0
$IFS
This variable is used to identify strings or word boundaries in Bash. The default value is a space. The value of this variable can be modified as needed in the script.
existingThe script, the content is as follows
#!/bin/bash va="a:b:c" vb="x-y-z" vc="e,f,g" IFS=":" echo 'va:'$va echo 'vb:'$vb echo 'vc:'$vc echo IFS="-" echo 'va:'$va echo 'vb:'$vb echo 'vc:'$vc echo IFS="," echo 'va:'$va echo 'vb:'$vb echo 'vc:'$vc
implement./
The results are as follows
[tt@ecs-centos-7 ~]$ ./
va:a b c
vb:x-y-z
vc:e,f,gva:a:b:c
vb:x y z
vc:e,f,gva:a:b:c
vb:x-y-z
vc:e f g
From the results, it can be seen that$IFS
for:
When string"a:b:c"
Been parsed intoa b c
when$IFS
for-
When string"x-y-z"
Been parsed intox y z
when$IFS
for,
When string"e,f,g"
Been parsed intoe f g
$HOME、$USER、$UID、$GROUPS
HOME: userhomeTable of contents USER: 当前user名 UID: 当前userID GROUPS: 当前user组ID
[tt@ecs-centos-7 ~]$ echo $HOME /home/tt [tt@ecs-centos-7 ~]$ echo $USER tt [tt@ecs-centos-7 ~]$ echo $UID 1003 [tt@ecs-centos-7 ~]$ echo $GROUPS 1003
$HOSTTYPE、$MACTYPE、$OSTYPE
These variables represent the system hardware
[tt@ecs-centos-7 ~]$ echo $HOSTTYPE x86_64 [tt@ecs-centos-7 ~]$ echo $MACHTYPE x86_64-redhat-linux-gnu [tt@ecs-centos-7 ~]$ echo $OSTYPE linux-gnu
summary
This article introduces some commonly used Bash built-in variables. For some uncommon or rarely used variables, you can check the relevant information by yourself.
The above is the detailed content of the commonly used built-in variables (Bash built-in variables) in shell scripts. For more information about built-in variables in shell, please pay attention to my other related articles!