shell腳本中$#、$0等的含義


1、$#

表示執行腳本傳入參數的個數

2、$*

表示執行腳本傳入參數的列表(不包括$0)

3、$$

表示進程的id

4、$@

表示執行腳本傳入參數的所有個數(不包括$0)

5、$0

表示執行的腳本名稱

6、$1

表示第一個參數

7、$@

表示第二個參數

8、$?

表示腳本執行的狀態,0表示正常,其他表示錯誤

例子:

!/bin/bash

printf "the process id is %s\n" "$$"

printf "the return value is %s\n" "$?"

printf "the all argus is %s\n" "$*"

printf "the argus is %s\n" "$@"

printf "the number of argus is %s\n" "$#"

printf "the first argus0 is %s\n" "$0"

printf "the argus 1 is %s\n" "$1"

printf "the argus 2 is %s\n" "$2"

執行結果

tay@tay:/mnt/hgfs/hzs/shell$ ./shell.sh 123 456

the process id is 5386

the return value is 0

the all argus is 123 456

the argus is 123

the argus is 456

the number of argus is 2

the first argus0 is ./shell.sh

the argus 1 is 123

the argus 2 is 456

2、$和$@的差異
在shell中,$@和$
都表示命令行所有的參數(不包含$0),但是$*將命令行所有的參數看成一個整體,而$@則區分各個參數

例子:

!/bin/bash

echo "the all para:"

for i in "$@"

do

echo $i #循環$#次

done

echo "the all para:"

for i in "$*"

do

echo $i

done

執行結果:

tay@tay:/mnt/hgfs/hzs/shell$ ./shell1.sh 1 2 3 4 5

the all para:

1

2

3

4

5

the all para:

1 2 3 4 5


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM