shell 脚本之特殊变量字符

shell 脚本之特殊变量字符

  • shell 脚本中我们知道变量都是以$符号开头的,但是有一些特殊的变量也很有用,这里我们就介绍一下有哪些比较实用的。
$0 这个脚本的执行名字
--------------------------
#! /bin/bash
echo $0
--------------------------
[root@localhost performance]# ./test.sh 
./test.sh
$n 这个程式的第n个参数值,n=1..9
-----------------------------------------
[root@localhost performance]# cat test.sh 
#! /bin/bash
echo $1 $2 $3
-----------------------------------------
[root@localhost performance]# ./test.sh hello world 123
hello world 123
$* 这个程式的所有参数,此选项参数可超过9个。 (所有参数组成一个字符串)
----------------------------------------------------------
#! /bin/bash
echo $*
----------------------------------------------------------
[root@localhost performance]# ./test.sh I love shell
I love shell
$@ 跟$*类似,但是可以当作数组用  (返回多个参数字符串)
------------------------------------------------
#! /bin/bash
echo $@
------------------------------
[root@localhost performance]# ./test.sh I love shell
I love shell
$# 这个程式的参数个数
------------------
#! /bin/bash
echo $#
------------------
[root@localhost performance]# ./test.sh I love shell
3
$? 执行上一个指令的返回值 (显示最后命令的退出状态。0表示没有错误,其他任何值表明有错误)
------------------------------------------------
#! /bin/bash
cd abc #打开不存在目录
echo $?
cd ./ #打开当前目录
echo $?
------------------------------
[root@localhost performance]# ./test.sh 
./test.sh: line 2: cd: abc: No such file or directory
1
0
$$ 这个程式的PID(脚本运行的当前进程ID号)
-----------------------------------
#! /bin/bash
echo $$
-----------------------------------
[root@localhost performance]# ./test.sh I love shell
22508
$! 执行上一个背景指令的PID(后台运行的最后一个进程的进程ID号)
$- 显示shell使用的当前选项,与set命令功能相同
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值