linux 脚本函数传参数,shell 函数(特定的函数和脚本内传参)

和其他脚本语言一样,bash同样支持函数。我们可创建特定的函数,也可以创建能够接受参数的函数。需要的时候直接调用就可以了。

1.定义函数

function fname()

{

statements;

}

或者:

fname()

{

statements;

}

只需要使用函数名,就可以调用某个函数

例如:

[root@gitlab script]# cat function_test.sh

#!/bin/bash

function fname()

{

echo "This is test function"; #;也可不用

}

for i in {1..5}

do

fname; #调用函数 ;也可不用

done

[root@gitlab script]# ./function_test.sh

This is test function

This is test function

This is test function

This is test function

This is test function

2.函数传参

[root@gitlab script]# cat function_test.sh

#!/bin/bash

function fname()

{

echo $1 $2 #打印参数

echo "$@" #以列表的方式一次性打印所有参数

echo "$*" #类似于$@,但单数被作为单个实体

}

fname hello world #传参

echo "##########################"

fname 1 2

[root@gitlab script]# ./function_test.sh

hello world

hello world

hello world

##########################

1 2

1 2

1 2

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值