• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >linux shell > Shell 函数参数

Shell 函数参数

作者:mdxy-dxy 字体:[增加 减小] 来源:互联网

mdxy-dxy 通过本文主要向大家介绍了shell 函数传参数进去,shell 带参数函数,shell 函数,shell 函数返回值,linux shell 函数等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

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

带参数的函数示例:

#!/bin/bash
funWithParam(){
  echo "The value of the first parameter is $1 !"
  echo "The value of the second parameter is $2 !"
  echo "The value of the tenth parameter is $10 !"
  echo "The value of the tenth parameter is ${10} !"
  echo "The value of the eleventh parameter is ${11} !"
  echo "The amount of the parameters is $# !"
  echo "The string of the parameters is $* !"
}
funWithParam 1 2 3 4 5 6 7 8 9 34 73
</div>

输出:

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}来获取参数。

另外,还有几个特殊字符用来处理参数:

参数处理 说明
$# 传递到脚本的参数个数
$* 以一个单字符串显示所有向脚本传递的参数
$$ 脚本运行的当前进程ID号
$! 后台运行的最后一个进程的ID号
$@ 与$#相同,但是使用时加引号,并在引号中返回每个参数。
$- 显示Shell使用的当前选项,与set命令功能相同。
$? 显示最后命令的退出状态。0表示没有错误,其他任何值表明有错误。

</div>

您可能想查找下面的文章:

  • Shell 函数参数

相关文章

  • 一句话Shell命令关闭不需要的随机启动服务
  • Shell字符串截取的详细方法
  • 一天一个shell命令 linux文本操作系列-tree命令详解
  • shell(bash)下“time” 命令的输出详解
  • shell 编程中空格的使用方法
  • shell实现数字打印从100到200的数
  • 分享一个入门级可控多线程shell脚本代码
  • git 使用及常用命令
  • 用expect实现的自动登录到多台服务器的shell脚本
  • shell脚本递归遍历目录及子目录的例子分享

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • Bash中数组的操作教程
    • 一天一个shell命令 linux文本内容操作系列-grep命令详解
    • linux下执行shell命令方法简介
    • 检查linux网络状态的两个脚本
    • 获取shell脚本自身所在目录的Shell脚本分享
    • shell脚本结合iptables防端口扫描的实现
    • linux shell流程控制语句实例讲解(if、for、while、case语句实例)
    • shell脚本监控linux系统内存使用情况的方法(不使用nagios监控linux)
    • 什么是Shell?Shell脚本基础知识详细介绍
    • linux 创建守护进程的相关知识

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有