16.2.2 指定位置参数值
除了在脚本运行时给脚本传入位置参数外,还可以使用内置命令set命令给脚本指定位置参数的值(又叫重置)。一旦使用set设置了传入参数的值,脚本将忽略运行时传入的位置参数(实际上是被set命令重置了位置参数的值)。
- [root@localhost ~]# cat set01.sh
#!/bin/bash
set 1 2 3 4 5 6 #
设置脚本的6
个位置参数,其值分别是1 2 3 4 5 6
COUNT=1
for i in $@
do
echo "Here \$$COUNT is $i"
let "COUNT++"
done
#
运行结果如下
#
给脚本传入参数a b c d e f
,但是由于脚本运行时“重置”了位置参数的值,
所以打印出来的位置参数为脚本中设置的值
[root@localhost ~]# bash set01.sh a b c d e f
Here $1 is 1
Here $2 is 2
Here $3 is 3
Here $4 is 4
Here $5 is 5
Here $6 is 6