在Shell中,调用函数时可以向其传递参数。在函数体内部,通过 $n 的形式来获取参数的值,例如,$1表示第一个参数,$2表示第二个参数...

带参数的函数示例:

  1. #!/bin/bash
  2. funWithParam(){
  3. echo "The value of the first parameter is $1 !"
  4. echo "The value of the second parameter is $2 !"
  5. echo "The value of the tenth parameter is $10 !"
  6. echo "The value of the tenth parameter is ${10} !"
  7. echo "The value of the eleventh parameter is ${11} !"
  8. echo "The amount of the parameters is $# !" # 参数个数
  9. echo "The string of the parameters is $* !" # 传递给函数的所有参数
  10. }
  11. funWithParam 1 2 3 4 5 6 7 8 9 34 73

运行脚本:

The value of the first parameter is 1 !
The value of the second parameter is 2 !
The value of the tenth parameter is 10 !
The value of the tenth parameter is 34 !
The value of the eleventh parameter is 73 !
The amount of the parameters is 12 !
The string of the parameters is 1 2 3 4 5 6 7 8 9 34 73 !"

注意,$10 不能获取第十个参数,获取第十个参数需要${10}。当n>=10时,需要使用${n}来获取参数。

另外,还有几个特殊变量用来处理参数,前面已经提到:

特殊变量 说明
$# 传递给函数的参数个数。
$* 显示所有传递给函数的参数。
$@ 与$*相同,但是略有区别,请查看Shell特殊变量
$? 函数的返回值。

最新文章

  1. 异常:“System.Reflection.Metadata”已拥有为“System.Collections.Immutable”定义的依赖项
  2. HNU 12868 Island (简单题)
  3. 让JS写的更接近OOP
  4. Bzoj-2820 YY的GCD Mobius反演,分块
  5. php开启错误提示
  6. [Python]豆瓣用户读书短评下载工具
  7. Indenting source code
  8. Java 相关注意事项小结
  9. 冲刺总结随笔(Alpha)
  10. [SCOI 2012]滑雪与时间胶囊
  11. (97)Wangdao.com_第三十天_触摸事件
  12. 关于thinkphp5URL重写
  13. linux学习之uniq
  14. xss脚本绕过限制的方法
  15. 解决Android studio生成H文件时报找不到类文件错误
  16. 关于Newtonsoft.Json,反序列化jason,内容有key的转换
  17. VHDL 乐曲演奏电路设计
  18. Linux安装Oracle 11g Grid Infrastructure 出现OUI-10182错误解决办法
  19. 【BZOJ】1002:轮状病毒(基尔霍夫矩阵【附公式推导】或打表)
  20. QWidget窗体中使用Q_OBJECT后无法添加背景图片或背景色

热门文章

  1. 25、转录本(transcript)组成(gtf文件的第三列)
  2. JavaScript学习系列7 -- JavaScript中的运算符
  3. Sharepoint2013商务智能学习笔记之Performancepoint service 配置(九)
  4. 【mybatis 如何写union和union查询】
  5. ProtoBuf练习(三)
  6. [CentOS7] 设备与文件名对应表
  7. python下一个转码的问题
  8. laravel 常见操作
  9. UPC11073(DP,思维)
  10. 在mac上使用sublime text3搭建opencv3开发环境