位置参数
Bash中的位置参数是由除0意外的一个或多个数字表示的参数。
位置参数是当Shell或Shell的函数被引用时由Shell或Shell函数的参数赋值,并且可以使用Bash的内部命令set来重新赋值。位置参数N可以被引用为$N,当N>=10时,需要用{}括起来。
如:
[root@rs1 test]# set 1 2 3 four five six 7 8 9 ten [root@rs1 test]# echo "$1 $2 $3 $4 ${10}" 1 2 3 four ten
位置参数不能通过赋值语句来赋值,而只能通过Bash的内部命令set和shift来设置和取消。当Shell函数运行时,位置参数会被临时地替换。
如:
[root@rs1 test]# cat show_positional_param.sh echo "Argument 1 : $1" echo "Argument 2 : $2" echo "Argument 3 : $3" echo "Argument 4 : $4" echo "Argument 5 : $5" [root@rs1 test]# bash show_positional_param.sh one two 3 4 coco Argument 1 : one Argument 2 : two Argument 3 : 3 Argument 4 : 4 Argument 5 : coco
特殊参数
Bash对一些参数的处理比较特殊,这些参数只能被引用,但不能修改它们的值。特殊参数都包含:*、@、#、?、-、$、!、0、_等
特殊参数 *
*表示从1开始所有位置的参数,如果扩展发生在双引号内,即"$*",则扩展包含每个参数值的单词,每个参数值用特殊表量IFS的第一个字符分割;也就是说,"$*"等价于"$1c$2c...",其中,c时特殊变量IFS的第一个字符。如果变量IFS没有定义,则参数之间默认用空格分割,如果IFS为空,则参数直接相连,中间没有分割
如:
[root@rs1 test]# set one two three [root@rs1 test]# echo $* one two three [root@rs1 test]# echo "$*" one two three
设置IFS为逗号:
[root@rs1 test]# IFS=, [root@rs1 test]# echo "$*" one,two,three [root@rs1 test]# echo $* one two three
设置IFS为空:
[root@rs1 test]# IFS= [root@rs1 test]# echo $* one two three [root@rs1 test]# echo "$*" onetwothree
特殊参数 @
@也扩展为从1开始的所有位置参数。但当它的扩展发生在双引号内时,每个参数都扩展为分割的单词。即:"$@"等价于"$1"、"$2" ...。参数@与*之间的区别会在for循环中体现出来
如:
[root@rs1 test]# set one two three [root@rs1 test]# IFS=, [root@rs1 test]# echo "$@" one two three [root@rs1 test]# echo $@ one two three
在for循环中的区别:
[root@rs1 test]# cat dif_arg.sh set one two three IFS=, for i in "$*" do echo "arguement : $i" done [root@rs1 test]# bash dif_arg.sh arguement : one,two,three [root@rs1 test]# cat dif_arg_2.sh set one two three IFS=, for i in "$@" do echo "arguement : $i" done [root@rs1 test]# bash dif_arg_2.sh arguement : one arguement : two arguement : three
特殊参数 ?
?扩展成为最近一个在前台执行的命令的退出状态。可以使用它来检查Shell脚本是否已经运行成功。通常0表示没有任何错误地结束运行。
如:
[root@rs1 test]# ls test1.sh test1.sh [root@rs1 test]# echo $? 0 [root@rs1 test]# jsifef -bash: jsifef: command not found [root@rs1 test]# echo $? 127 //错误返回 [root@rs1 test]# ls fsx ls: cannot access fsx: No such file or directory [root@rs1 test]# echo $? 2
不同的返回值代表不同含义,具体含义,可以参考:
特殊参数 -
-扩展为当前的选项标志。这些选项是在调用时,或由内部命令set指定,或由Shell自身指定
特殊参数 $
$扩展为当前Shell的进程号。在一个子Shell中,它扩展为调用Shell的进程号,而不是子Shell的进程号。
如:
[root@rs1 test]# echo $$ 2140 [root@rs1 test]# ps -ax |grep pts/0 2138 ? Ss 0:00 sshd: root@pts/0 2140 pts/0 Ss 0:00 -bash 2303 pts/0 R+ 0:00 ps -ax 2304 pts/0 S+ 0:00 grep --color=auto pts/0
特殊参数 !
!扩展为最近一次执行命令的后台命令的进程号。
如:
[root@rs1 test]# sleep 10 & [1] 2325 [root@rs1 test]# echo $! 2325
特殊参数 0
0 扩展为Shell或Shell脚本名称。在Shell初始化时设置。如果Bash调用时带有脚本文件作为参数,则可设置$0作为参数表示文件名。
[root@rs1 test]# cat param_zero.sh #!/bin/bash echo "The \$0 is $0" echo "The \$1 is $1" [root@rs1 test]# bash param_zero.sh fsx The $0 is param_zero.sh The $1 is fsx
特殊参数 _
_,在Shell启动时,被设置为开始运行的Shell或Shell脚本的路径。之后扩展为前一个命令的最后一个参数。
如:
[root@rs1 test]# cat param_underscore.sh #!/bin/bash echo "The \$_ is $_ , when first time running this script" uname -r echo "After some commands,The \$_ is $_" [root@rs1 test]# bash param_underscore.sh The $_ is /usr/bin/bash , when first time running this script 3.10.0-123.el7.x86_64 After some commands,The $_ is -r