在shell中参数的传递,也就是参数的输入使用$n
的格式进行输入。
$n
,其中,n代表数字,1就是在执行脚本时输入的第一个参数,2就是在执行脚本时输入的第一个参数,以此类推。
#!/bin/bash
#Filename: Day02.sh
#Introduction:It's file is to learning the basic knowledge of shell
echo "Output"
echo "The file name: $0"
echo "The first parameter: $1"
echo "The second parameter: $1"
echo "The number of paramter is $#"
leeguo@leeguo-PC:~/Nutstore/Shell_code$ bash Day02.sh 1 2
Output
The file name: Day02.sh
The first parameter: 1
The second of paramter is 2
这个符号$
,除了参数的传输外,还有其他的功能。
-
0:当前文件的名字−
#:当前脚本输入参数的个数
- $*:以字符串的形式显示所有输入的参数,引号"$*"
是一一个字符串的形式输出形式进行输出
- $@:和上面"$*"
的功能基本相同,都是在引号"$@"
是一单个的输出形式进行输出
- $$:该脚本进程的ID号
- $?:脚本在运行完成后,退出的状态,若为0则说明没有错误
#!/bin/bash
#Filename: Day02.sh
#Introduction:It's file is to learning the basic knowledge of shell
echo "$*"
echo "$@"
for target in "$*";
do
echo $target
done
for target in "$@";
do
echo $target
done
运行的结果:
leeguo@leeguo-PC:~/Nutstore/Shell_code$ bash Day02.sh 1 2 3
# The output of $*
1 2 3
# The output of $@
1 2 3
# --------
1 2 3
# --------
1
2
3
本文参考:http://www.runoob.com/linux/linux-shell-passing-arguments.html