所谓的位置参数指的也就是shell脚本的命令行参数。在shell函数里面,它们也可以同时是函数的参数。各参数都由整数命名,但超过了9就应该用大括号把数字框起来。
[badboy@localhost ~]$ set -- 1 2 3 4 5 6 7 8 9 10
[badboy@localhost ~]$ echo the first number is $1
the first number is 1
[badboy@localhost ~]$ echo the tenth number is ${10}
the tenth number is 10
下面我主要介绍的是特殊“变量”提供对传递的参数的总数的访问,以及一次对所有参数的访问。
1. $#
提供传递到shell脚本或函数的参数总数。当你为了处理选项和参数而建立循环时会很有用哈。。
[badboy@localhost ~]$ echo the number of arg is $#
the number of arg is 10
2. $*,$@
一次表示所有的命令行参数。
[badboy@localhost ~]$ echo all number is $*
all number is 1 2 3 4 5 6 7 8 9 10
[badboy@localhost ~]$ echo all number is $@
all number is 1 2 3 4 5 6 7 8 9 10
3."$*"
将所有的命令行参数视为单个字符串。等同于"$1 $2 $3..."。
[badboy@localhost ~]$ for i in "$*"
> do echo i is $i
> done
i is 1 2 3 4 5 6 7 8 9 10
4. "$@"
将所有命令行参数视为单个字符串,等同于"$1","$2","$3"....。。
[badboy@localhost ~]$ for i in "$@"
> do echo i is $i
> done
i is 1
i is 2
i is 3
i is 4
i is 5
i is 6
i is 7
i is 8
i is 9
i is 10
阅读(2651) | 评论(0) | 转发(0) |