天天看點

linux中shell變量$#,[email protected],$0,$1,$2的含義解釋

摘抄自:ABS_GUIDE

下載下傳位址:http://www.tldp.org/LDP/abs/abs-guide.pdf

linux中shell變量$#,[email protected],$0,$1,$2的含義解釋: 

變量說明: 

$$ 

Shell本身的PID(ProcessID) 

$! 

Shell最後運作的背景Process的PID 

$? 

最後運作的指令的結束代碼(傳回值) 

$- 

使用Set指令設定的Flag一覽 

$* 

所有參數清單。如"$*"用「"」括起來的情況、以"$1 $2 … $n"的形式輸出所有參數。 

[email protected] 

所有參數清單。如"[email protected]"用「"」括起來的情況、以"$1" "$2" … "$n" 的形式輸出所有參數。 

$# 

添加到Shell的參數個數 

$0 

Shell本身的檔案名 

$1~$n 

添加到Shell的各參數值。$1是第1參數、$2是第2參數…。 

示例:

1 #!/bin/bash

2 #

3 printf

"The complete list is %s\n"

"$$"

4 printf

"The complete list is %s\n"

"$!"

5 printf

"The complete list is %s\n"

"$?"

6 printf

"The complete list is %s\n"

"$*"

7 printf

"The complete list is %s\n"

"[email protected]"

8 printf

"The complete list is %s\n"

"$#"

9 printf

"The complete list is %s\n"

"$0"

10 printf

"The complete list is %s\n"

"$1"

11 printf

"The complete list is %s\n"

"$2

結果:

[[email protected] ~]$ bash

params

.sh 123456 QQ

The complete list

is

24249

The complete list

is

The complete list

is

The complete list

is

123456 QQ

The complete list

is

123456

The complete list

is

QQ

The complete list

is

2

The complete list

is

params

.sh

The complete list

is

123456

The complete list

is

QQ

繼續閱讀