Shell must know and know | 2. The special shell characters you need are here!

  table of Contents

1. Shell special characters

1.1 $N 

1.2 $#

1.3 $*

1.5 $?

1.6 $$

1.7 $!

1.8 $-

Two, summary


When writing Shell scripts, some special characters are usually used, such as: $?, $@, etc. This article will introduce all Shell special characters with examples. It is recommended to collect them first!

1. Shell special characters

1.1 $N 

N represents a number, where $0 represents the current script file name, and $N (N> 0) represents the Nth input parameter entered into the script, as shown below:

[root@localhost ~]# cat shell.sh
#!/bin/bash

echo $0 $1 $2 $3 $4 $5 $6 $7 $8 $9 ${10}

[root@localhost ~]# ./shell.sh 1 2 3 4 5 6 7 8 9 10
./shell.sh 1 2 3 4 5 6 7 8 9 10
[root@localhost ~]#

Note: The output of $0 is ./shell.sh.

1.2 $#

The number of parameters passed to the script or function is as follows:

[root@localhost ~]# cat shell.sh
#!/bin/bash

echo $#
[root@localhost ~]# ./shell.sh 1 2 3
3
[root@localhost ~]#
[root@localhost ~]# cat shell.sh 
#!/bin/bash

funcTest() {
    echo $#
}

funcTest 1 2 3 4
[root@localhost ~]# ./shell.sh 
4
[root@localhost ~]# 

1.3 $*

 Represents all parameters passed to the script or function, as shown below:

[root@localhost ~]# cat shell.sh 
#!/bin/bash

echo $*
[root@localhost ~]# ./shell.sh 1 2 3 4 5
1 2 3 4 5
[root@localhost ~]#
[root@localhost ~]# cat shell.sh 
#!/bin/bash

funcTest() {
    echo $*
}

funcTest 1 2 3 4
[root@localhost ~]# ./shell.sh 
1 2 3 4
[root@localhost ~]#

1.4 $@

Represents all parameters passed to the script or function, as shown below:

[root@localhost ~]# cat shell.sh 
#!/bin/bash

echo $@
[root@localhost ~]# ./shell.sh 1 2 3 4 5
1 2 3 4 5
[root@localhost ~]#
[root@localhost ~]# cat shell.sh 
#!/bin/bash

funcTest() {
    echo $@
}

funcTest 1 2 3 4
[root@localhost ~]# ./shell.sh 
1 2 3 4
[root@localhost ~]#

 The difference between $@ and $* :

  • When not enclosed by double quotation marks, the two symbols are the same, and both are output in the form of $1 $2 $3 ...... separated;
  • When enclosed in double quotes, $* will output all parameters as a whole, for example: "$1 $2 $3 ……", and $@ will separate each parameter, for example: $1 $2 $3 ……;

Let's see through an example:

First look at the same situation, as shown below:

[root@localhost ~]# cat shell.sh 
#!/bin/bash

echo "\$*"
for i in $*
do
    echo $i
done

echo "\$@"
for i in $@
do
    echo $i
done
[root@localhost ~]# ./shell.sh 1 2 3 4 5
$*
1
2
3
4
5
$@
1
2
3
4
5
[root@localhost ~]#

Look at the difference, as shown below:

[root@localhost ~]# cat shell.sh 
#!/bin/bash

echo "\$*"
for i in "$*"
do
    echo $i
done

echo "\$@"
for i in "$@"
do
    echo $i
done
[root@localhost ~]# ./shell.sh 1 2 3 4 5
$*
1 2 3 4 5
$@
1
2
3
4
5
[root@localhost ~]# 

In the above example, all the parameters contained in $* enclosed by double quotation marks are regarded as a whole, and "1 2 3 4 5" is directly output, and the parameters in $@ are still separated.

1.5 $?

Indicates the exit status of the previous command or the return value of the function, as shown below:

[root@localhost ~]# cat shell.sh 
#!/bin/bash

pwd
echo $?

ls -l /root/abc
echo $?
[root@localhost ~]# ./shell.sh 
/root
0
ls: 无法访问/root/abc: 没有那个文件或目录
2
[root@localhost ~]#

The value of the first $? is 0, indicating that the command pwd is executed successfully; the value of the second $? is 2, indicating that the last command ls failed to execute.

[root@localhost ~]# cat shell.sh 
#!/bin/bash

funcTest(){
    return 6
}

funcTest
echo $?
[root@localhost ~]# ./shell.sh 
6
[root@localhost ~]#
[root@localhost ~]# cat shell.sh 
#!/bin/bash

funcTest(){
    echo "abc"
}

val=$(funcTest)
echo $?
[root@localhost ~]# ./shell.sh 
0
[root@localhost ~]#

When $? is used on a function, if there is a return statement, the value returned by the return statement is obtained, and the range is 0~255. If there is no return statement, the value of $? represents the return code of the last command executed in the function.

1.6 $$

Represents the ID number of the current process of the script, as shown below: 

[root@localhost ~]# cat shell.sh 
#!/bin/bash

echo $$
[root@localhost ~]# ./shell.sh 
26033
[root@localhost ~]# 

1.7 $!

Represents the ID number of the last running process in the background , and can also be understood as the ID number of the most recently running process, which is not necessarily finished.

[root@localhost ~]# top &
[2] 31702
[root@localhost ~]# echo $!
31702

[2]+  已停止               top
[root@localhost ~]#

1.8 $-

Display the current options used by the Shell command, which has the same function as the set command, as shown below:

[root@localhost ~]# cat shell.sh 
#!/bin/bash

set -m
echo $-

set -C
echo $-
[root@localhost ~]# ./shell.sh 
hmB
hmBC
[root@localhost ~]#

 Note: In the second output, the parameter C just set is added.

Two, summary

Well, the explanation of Shell special characters combined with examples is finished. If it feels good, just one-click triple connection. If you have any questions, please leave a message in the comment area~

Guess you like

Origin blog.csdn.net/u011074149/article/details/112797011