特殊字符$在Shell编程中的含义
Shell命令特殊字符 | |
内置变量 |
含义 |
$? |
最后一次执行的命令的返回码 |
$$ |
Shell进程自己的PID |
$! |
Shell进程最近启动的后台进程的PID |
$# |
命令行参数的个数(不包括脚本文件的名字在内) |
$0 |
脚本文件本身的名字 |
$1, $2, … … |
第一个,第二个,… …,命令行参数 |
$* |
“$1, $2, … …”,将所有命令含参数组织成一个整体,作为一个单词 |
$@ |
$1, $2, … …,将多个命令行参数看作是多个“单词” |
脚本源码testDollar.sh
#!/bin/bash
# Createn on 2012/10/16 20:50
# By Unee_Yu/优快云
# OS: Ubuntu 12.04.1
# Desc: The use of $ in sh file
declare -i i=1;
echo "0: This is a sh file which try to show the use of $."
echo "==============================================================="
echo "1: Use command 1 \"ls -al\" to list all files and directory in current folder."
ls -al
echo "The result code of command 1:$?"
echo "==============================================================="
echo "2: PID of the Shell process: $$"
echo "==============================================================="
echo "3: Create one file named \"temp.txt\" in daemon mode"
touch temp.txt &
echo "PID of the last daemon process run by Shell: $!"
echo "==============================================================="
echo "4: Number of command line arguments: $#"
echo "==============================================================="
echo "5: Name of this sh file: $0"
echo "==============================================================="
echo '6: $1 = '"$1"
echo "==============================================================="
echo "7: List all arguments:"
for para in $@
do
echo "para $i: $para"
i=i+1
done
echo "==============================================================="
echo "8: Print all arguments as one string: $*"
运行结果
运行命令:./testDollar.sh p1 p2 p3 p4
0: This is a sh file which try to show the use of $.
===============================================================
1: Use command 1 "ls -al" to list all files and directory in current folder.
total 96
drwxrw-r-- 5 unee unee 4096 Oct 16 19:57 .
drwxr-xr-x 25 unee unee 4096 Oct 16 19:57 ..
-rw-rw-r-- 1 unee unee 194 Oct 16 19:57 result.txt
drwxrwxr-x 3 unee unee 4096 Oct 15 23:43 sh_ex1
drwxrwxr-x 2 unee unee 4096 Oct 16 00:41 sh_ex2
drwxrwxr-x 2 unee unee 4096 Oct 16 01:11 sh_ex3
-rw-rw-r-- 1 unee unee 1106 Oct 16 19:52 source_result.txt
-rwxrw-r-- 1 unee unee 103 Oct 15 23:59 template.sh
-rw-rw-r-- 1 unee unee 0 Oct 16 19:57 temp.txt
-rwxrw-r-- 1 unee unee 1301 Oct 16 19:57 testDollar.sh
-rw-rw-r-- 1 unee unee 14 Oct 14 23:47 xargs.log
-rw-rw-r-- 1 unee unee 56931 Oct 14 22:48 x.log
lrwxrwxrwx 1 unee unee 9 Oct 14 23:44 xs.log -> xargs.log
The result code of command 1:0
===============================================================
2: PID of the Shell process: 2331
===============================================================
3: Create one file named "temp.txt" in daemon mode
PID of the last daemon process run by Shell: 2333
===============================================================
4: Number of command line arguments: 4
===============================================================
5: Name of this sh file: ./testDollar.sh
===============================================================
6: $1 = p1
===============================================================
7: List all arguments:
para 1: p1
para 2: p2
para 3: p3
para 4: p4
===============================================================
8: Print all arguments as one string: p1 p2 p3 p4