Shell脚本中$的用法
2015-08-02 23:07
393 查看
$0: 脚本名称,相对路径还是绝对路径要看调用的方式。如果是在父目录中,./dollarTest.sh调用,那么$0就是./dollarTest.sh;如果是全路径调用,如"/root/test/dollarTest.sh",那么$0就是"/root/test/dollarTest.sh"。
$n:调用的第n个参数。ps:如果n > 9,那么数字需要用括号括起来,如${10},否则shell解释器会认为是$1加上1这个数字。
$*:脚本中的所有参数。
$@:同$*。
$#:脚本的参数个数。
$$:改shell脚本执行时的进程号。
$?:上一条命令的输出结果,如果上条命令exit的返回值。
$!:上一条后台进程执行的pid号。
!$:上一条命令的最后一个字符串。
$-:使用set命令设定的flag。
以运行脚本dollarTest.sh为例,脚本代码:
![](http://img.blog.csdn.net/20150802232357381)
运行命令及参数:./dollarTest.sh 1 2 3 4 5 6 7 8 9 10 21 22 23 24
执行结果:
$n:调用的第n个参数。ps:如果n > 9,那么数字需要用括号括起来,如${10},否则shell解释器会认为是$1加上1这个数字。
$*:脚本中的所有参数。
$@:同$*。
$#:脚本的参数个数。
$$:改shell脚本执行时的进程号。
$?:上一条命令的输出结果,如果上条命令exit的返回值。
$!:上一条后台进程执行的pid号。
!$:上一条命令的最后一个字符串。
$-:使用set命令设定的flag。
以运行脚本dollarTest.sh为例,脚本代码:
#!/bin/bash echo '$0 is ' $0 echo '$1 is ' $1 echo '$11 is ' ${11} echo '$# is ' $# echo '$* is ' $* echo '$@ is ' $@ echo '$$ is ' $$ sh test.sh # this shell script will exit with 11 echo '$? is ' $? echo '$# is ' $# nohup ping www.baidu.com & echo '$! is ' $! echo $-因有特殊字符,显示不全,故用图片显示shell源码;
运行命令及参数:./dollarTest.sh 1 2 3 4 5 6 7 8 9 10 21 22 23 24
执行结果:
相关文章推荐
- Linux Shell 1>/dev/null 2>&1 含义
- shell内部命令-----变量类
- scala的REPL shell的调用
- scala的REPL shell的调用
- 收集的55个Linux系统管理中常用的一些shell命令
- Shell 学习17 - Shell for 循环
- Shell 学习16 - Shell case esac 语句
- Shell 学习15 - Shell if else 语句
- Shell 学习14 - Shell printf 命令
- shift+鼠标右键文件夹打开命令行直接进入当前目录
- shell小技巧--用“$$”和"$0"防止同一个脚本启动多次
- shell中引用其他脚本的方法
- shell各种执行方式区别
- my_shell 编写
- shell编程
- Shell中创建命令别名
- Shell编程初涉--一些关于Shell的整理
- Shell编程初涉--一些关于Shell的整理
- How PowerShell Differs From the Windows Command Prompt
- 命令被shell处理过程