shell函数参数

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

 带参数的函数示例: 

01.#!/bin/bash
02.funWithParam(){
03.    echo "The value of the first parameter is $1 !"
04.    echo "The value of the second parameter is $2 !"
05.    echo "The value of the tenth parameter is $10 !"
06.    echo "The value of the tenth parameter is ${10} !"
07.    echo "The value of the eleventh parameter is ${11} !"
08.    echo "The amount of the parameters is $# !"  # 参数个数
09.    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特殊变量。 
$? 函数的返回值。 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值