Shell中的特殊字符$的含义以及实例

本文详细介绍了Shell脚本中特殊字符$的功能及其应用场景,包括如何获取命令执行结果、进程ID、命令行参数等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

特殊字符$在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


 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值