passing parameters shell function in several ways

1. recently summed up the transfer function of the variable shell in several ways
1. The single variable transmission
2. pass the array variable
 
#!/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=(hello world)
echo "***************************************************"
func ${array[*]} ${strarray[*]}
func ${array[@]} ${strarray[@]}
 
echo "*****************************************************"
func2 array[@] strarray[@]
func2 array[*] strarray[*]
 
Output:
****************************************************
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

Guess you like

Origin www.cnblogs.com/idyllcheung/p/11099209.html