Linux中变量$#,$@,$0,$1,$2,$*,$$,$?的含义

来源:互联网 发布:enum class java 编辑:程序博客网 时间:2024/05/01 01:02
Linux中变量$#,$@,$0,$1,$2,$*,$$,$?的含义:
$# 是传给脚本的参数个数
$0 是脚本本身的名字
$1 是传递给该shell脚本的第一个参数
$2 是传递给该shell脚本的第二个参数
$@ 是传给脚本的所有参数的列表
$* 是以一个单字符串显示所有向脚本传递的参数,与位置变量不同,参数可超过9个
$$ 是脚本运行的当前进程ID号
$? 是显示最后命令的退出状态,0表示没有错误,其他表示有错误

exmp:
[kernel@localhost test]$ cat varible 
#!/bin/sh
echo "---------------------------------------"
echo "The number of parameters: $#"
echo "The name of the script itself: $0"
echo "First parameter: $1"
echo "second parameter: $2"
echo "All parameters: $@"
echo "Show parameters list: $*"
echo "Show process id: $$"
echo "Show exit stat: $?"
echo "---------------------------------------"


运行结果:

[kernel@localhost test]$ sh varible hello word
---------------------------------------
The number of parameters: 2
The name of the script itself: varible
First parameter: hello
second parameter: word
All parameters: hello word
Show parameters list: hello word
Show process id: 10628
Show exit stat: 0
---------------------------------------
0 0
原创粉丝点击