一:shell特殊變量
1. 位置變量
$0 獲取當前執行的shell腳本的文件名,包括路徑
$n 獲取當前執行的shell腳本的第n個參數值,n=1..9,當n為0時表示腳本的文件名,如果n大於9,用大括號括起來${10}
$* 獲取當前shell的所有參數,將所有的命令行參數視為單個字符串,相當於"$1$2$3"........注意與$#的區別
$# 獲取當前shell命令行中的參數的總個數
$@ 這個程序的所有參數"$1" "$2" "$3" ".....",這是將參數傳遞給其他程序的最佳方式,因為他會保留所有內嵌在每個參數里的任何空白。
提示:$*和$@的區別?
2.案例、演示
范例1:演示$0
[root@1-241 scripts]# cat 0.sh echo $0 [root@1-241 scripts]# sh 0.sh 0.sh [root@1-241 scripts]# cd /root/ [root@1-241 ~]# sh /scripts/0.sh /scripts/0.sh 范例2:演示$1到$10的作用 [root@1-241 scripts]# cat n.sh echo $1 $2 $3 $4 $5 $6 $7 $8 $9 ${10} [root@1-241 scripts]# sh n.sh `seq 10` 1 2 3 4 5 6 7 8 9 10 [root@1-241 scripts]# sh n.sh `seq 8` 1 2 3 4 5 6 7 8 [root@1-241 scripts]# sh n.sh `seq 12` 1 2 3 4 5 6 7 8 9 10 提示: 在腳本里寫入$1到$10,在腳本執行傳參,傳入最大是10個參數,超過10個參數也不接收 范例3:演示$#的作用 [root@1-241 scripts]# cat n.sh echo $1 $2 $3 $4 $5 $6 $7 $8 $9 ${10} echo $# [root@1-241 scripts]# sh n.sh 1 2 3 4 5 1 2 3 4 5 5 提示 $#,顯示傳入了多少個參數
