範例:
執行 positional_param.sh 並傳入五個參數 a b c d e 如下
./positional_param.sh a b c d e
#!/bin/sh
echo \$0 = $0
echo \$1 = $1
echo \$2 = $2
echo \$3 = $3
echo \$4 = $4
echo \$5 = $5
echo ""
showParams()
{
echo "In showParams"
echo \$0 = $0
echo \$1 = $1
echo \$2 = $2
echo \$3 = $3
echo \$4 = $4
echo \$5 = $5
echo \$6 = $6
}
showParams red orange yellow green blue purple
$0 = ./positional_param.sh
$1 = a
$2 = b
$3 = c
$4 = d
$5 = e
In showParams
$0 = ./positional_param.sh
$1 = red
$2 = orange
$3 = yellow
$4 = green
$5 = blue
$6 = purple
說明:
1. 從範例可看出,位置參數對應到傳給 positional_param.sh 的參數。
2. 而 showParams 裡的位置參數會變成傳給 showParams 的參數。
3. $0 比較特別,在 showParams 裡還是 positional_param.sh。
沒有留言:
張貼留言