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 篇原创文章 · 获赞 3 · 访问量 1471

猜你喜欢

转载自blog.csdn.net/hemaobin/article/details/88910744