Bash Shell 中会用到一些参数,
$1,$2,... 特定第几个参数, $0 表示当前执行的进程名,script 本身的名字,或者在正则表达式中表示整行输出
$#
命令行或者是位置参数的个数
$*
所有的位置参数,被作为一个单词.
$@
与$*同义,但是每个参数都是一个独立的""引用字串,这就意味着参数被完整地传递
$- 传递给脚本的falg
$! 在后台运行的最后的工作的PID(进程ID).
$_ 保存之前执行的命令的最后一个参数.
$? 函数或者脚本本身的退出状态,用于检查上一个命令,函数或者脚本执行是否正确。(0表示该命令正确执行,任何非0值表示命令出错。)
$$ 脚本自身的进程ID.
使用举例:
执行语句运行结果:
[root@myhost tmp]# ./filelist.sh file1.txt file2.txt file3.txt
general file list files: 3
General by: ./filelist.sh
Last command: Last PID:
Is normal: 0
Current PID: 26418
general file list files: 3
General by: ./filelist.sh
Last command: Last PID:
Is normal: 0
Current PID: 26418
filelist.txt:
[root@myhost tmp]# cat filelist.txt
Param1: file1.txt
Param2: file2.txt
All param: file1.txt file2.txt file3.txt
All param: file1.txt file2.txt file3.txt
Flag: hB
Last PID:
Param1: file1.txt
Param2: file2.txt
All param: file1.txt file2.txt file3.txt
All param: file1.txt file2.txt file3.txt
Flag: hB
Last PID:
-------------------------------------------------
filelist.sh
[root@myhost tmp]# cat filelist.sh
#!/bin/bash
echo "general file list files: $#"
echo "General by: $0"
echo "Param1: $1" > filelist.txt
echo "Param2: $2" >> filelist.txt
echo "All param: $*" >> filelist.txt
echo "All param: $@" >> filelist.txt
echo "Flag: $-" >> filelist.txt
echo "Last PID: $!" >> filelist.txt
echo "Last command: $_"
echo "Is normal: $?"
echo "Current PID: $$"
#!/bin/bash
echo "general file list files: $#"
echo "General by: $0"
echo "Param1: $1" > filelist.txt
echo "Param2: $2" >> filelist.txt
echo "All param: $*" >> filelist.txt
echo "All param: $@" >> filelist.txt
echo "Flag: $-" >> filelist.txt
echo "Last PID: $!" >> filelist.txt
echo "Last command: $_"
echo "Is normal: $?"
echo "Current PID: $$"
转载于:https://blog.51cto.com/babyhe/322297