Linux shell 函数传递参数的几种方式

最近总结了 shell 中 function 的传递变量的几种方式

1.传递单个变量
2.传递数组变量

#!/bin/bash
#trying to pass an variable.

function func()
{
echo 'The number of parameters is: ${#}'
for line in '$@'
do
echo '$line'
done
}

function func2()
{
param1=('${!1}')
param2=('${!2}')
echo ${param1[*]}
echo ${param2[*]}
}
echo '****************************************************'
#1.pass simpl variable.
func 'hello'
func 'hello' 'world'
func 1 2 3

#2.pass array variable
echo '*****************************************************'
array=(1 2 3)
strarray

输出结果如下:


The number of parameters is: 1
hello
The number of parameters is: 2
hello
world
The number of parameters is: 3
1
2
3



The number of parameters is: 5
1
2
3
hello
world
The number of parameters is: 5
1
2
3
hello
world


1 2 3
hello world
1 2 3
hello world

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值