最近总结了 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