Linux中的位置参数变量

解释:
  主要是向脚本中传递数据,变量名不能自定义,变量作用是固定的

$n
  $0代表命令本身,$1-9代表接受的第1-9个参数,10以上需要用{}括起来,比如${10}代表接收的第10个参数
$*
  代表接收所有的参数,将所有参数看作一个整体
$@
  代表接收的所有参数,将每个参数区别对待
$#
  代表接收的参数个数

例子:
[root@localhost sh]# vi param_test.sh
[root@localhost sh]# cat param_test.sh
#!/bin/bash

echo $0
echo $1
echo $2
echo $#
[root@localhost sh]# sh param_test.sh xx yy
param_test.sh
xx
yy
2

[root@localhost sh]# vi param_test2.sh
[root@localhost sh]# cat param_test2.sh
#!/bin/bash

for x in "$*"
  do
    echo $x
  done

for y in "$@"
  do
    echo $y
  done
[root@localhost sh]# sh param_test2.sh 1 2 3
1 2 3
1
2
3
[root@localhost sh]#

posted @ 2017-07-11 23:11  413Xiaol  阅读(4148)  评论(0编辑  收藏  举报