$0: 代表第一个参数,通常是脚本本身的名字
$1: 代表第二个参数
...
以此类推
例如:
#!/bin/bash
echo "\$0="$0
echo "\$1="$1
echo "\$2="$2
运行结果:
$ ./a.sh a b
$0=./a.sh
$1=a
$2=b
$@
表示从第一到第n个参数,例如:
#!/usr/bin/bash
for arg in $@
do
echo $arg
done
运行结果:
$ ./a.sh a b c
a
b
c
还有,当入参不存在的时候,可以有缺省值,例如:
#!/usr/bin/bash
para1=${1:-xxx}
echo $para1
运行结果:
$ ./c.sh abc
abc
$ ./c.sh
xxx
更多内容,定义这里,感兴趣的自己看吧:
https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html
${parameter:-[word]}
Use Default Values. If parameter is unset or null, the expansion of word (or an empty string if word is omitted) shall be substituted; otherwise, the value of parameter shall be substituted.
${parameter:=[word]}
Assign Default Values. If parameter is unset or null, the expansion of word (or an empty string if word is omitted) shall be assigned to parameter. In all cases, the final value of parameter shall be substituted. Only variables, not positional parameters or special parameters, can be assigned in this way.
${parameter:?[word]}
Indicate Error if Null or Unset. If parameter is unset or null, the expansion of word (or a message indicating it is unset if word is omitted) shall be written to standard error and the shell exits with a non-zero exit status. Otherwise, the value of parameter shall be substituted. An interactive shell need not exit.
${parameter:+[word]}
Use Alternative Value. If parameter is unset or null, null shall be substituted; otherwise, the expansion of word (or an empty string if word is omitted) shall be substituted.