原文网址:http://www.cnblogs.com/fhefh/archive/2011/04/15/2017613.html

linux中shell变量$#,$@,$0,$1,$2的含义解释:
变量说明:
$$
Shell本身的PID(ProcessID)
$!
Shell最后运行的后台Process的PID
$?
最后运行的命令的结束代码(返回值)
$-
使用Set命令设定的Flag一览
$*
所有参数列表。如"$*"用「"」括起来的情况、以"$1 $2 … $n"的形式输出所有参数。
$@
所有参数列表。如"$@"用「"」括起来的情况、以"$1" "$2" … "$n" 的形式输出所有参数。
$#
添加到Shell的参数个数
$0
Shell本身的文件名
$1~$n
添加到Shell的各参数值。$1是第1参数、$2是第2参数…。
示例:

1 #!/bin/bash
2 #
3 printf "The complete list is %s\n" "$$"
4 printf "The complete list is %s\n" "$!"
5 printf "The complete list is %s\n" "$?"
6 printf "The complete list is %s\n" "$*"
7 printf "The complete list is %s\n" "$@"
8 printf "The complete list is %s\n" "$#"
9 printf "The complete list is %s\n" "$0"
10 printf "The complete list is %s\n" "$1"
11 printf "The complete list is %s\n" "$2
结果:

[Aric@localhost ~]$ bash params.sh 123456 QQ
The complete list is 24249
The complete list is
The complete list is 0
The complete list is 123456 QQ
The complete list is 123456
The complete list is QQ
The complete list is 2
The complete list is params.sh
The complete list is 123456
The complete list is QQ
Have a nice day!!!

补充:

$@与$*的区别

(1)
#!/bin/sh
#test.sh
for i in $@  #for i in $*
do
  echo $i
done

测试:test.sh 1 2 3 
结果一样,都输出:
1
2
3

(2)
for i in "$@"  #for i in "$*"
do
  echo $i
done

测试:test.sh 1 2    3

第一个输出:
1
2
3

第二个输出:1 2 3

随机推荐

  1. jsp获取绝对路径

    在JavaWeb开发中,常使用绝对路径的方式引入javaScript和CSS文件,这样可以避免因为目录变动导致引入文件找不到的情况,常用的做法是: 一.使用${pageContext.request. ...

  2. iOS JavaScriptCore使用

    iOS JavaScriptCore使用 JavaScriptCore是iOS7引入的新功能,JavaScriptCore可以理解为一个浏览器的运行内核,使用JavaScriptCore可以使用nat ...

  3. java反射调用api

    cglib的fastmethod 简单示例: FastClass serviceFastClass = FastClass.create(Person.class); Person p = new P ...

  4. 【待填坑】ajax问题

    原生xhr怎么写? 怎么处理回调? 问:http状态码常见有哪些? 问:302是啥?304是啥?什么时候会返回304?你刚刚说浏览器缓存,具体缓存机制是怎么样的? 问:你刚刚说的是发起一个get请求, ...

  5. BOM-event事件

    添加事件监听 <button id="btnShoot">shoot</button><br> <button id="btnA ...

  6. 验证email是否合法

    https://buluo.qq.com/p/detail.html?bid=339910&pid=6675390-1514450689&from=grp_sub_obj 场景1:验证 ...

  7. redis的Hash类型以及其操作

    hashes类型 hashes类型及操作Redis hash是一个string类型的field和value的映射表.它的添加.删除操作都是0(1)(平均).hash特别适合用于存储对象.相较于将对象的 ...

  8. 对于get方法是否需要synchronized修饰

    具体用法没有总结,只是说明一个用法而已,对于以前个人理解出现的偏差 [问题描述] 对于一个计数功能的实现,获取值的方法是否需要加锁? [以前理解] 我只需要在进行累加的方法上进行加锁即可,这样保证其可 ...

  9. 收集hive优化解决方案

    hive的优化问题1.启动一次JOB尽可能多做事,尽量减少job的数量.能重用就重用,要设计好的模型.2.合理设置reduce个数,reduce个数过多,会造成大量小文件问题.3.使用hive.exe ...

  10. EasyDarwin如何支持点播和RTMP/HLS直播?EasyDSS!

    2017年很长很长一段时间没有更新EasyDarwin开源项目了,虽然心里有很多EasyDarwin功能扩展的计划:比如同步录像.同步RTMP/HLS直播输出.拉模式转发优化.Onvif接入.GB28 ...