I am passing an array to a function and trying to print each and every element of the array.
Below is the code snippet with quotes around the array parameter:
#!/bin/bash
print_array ()
{
array=$@
for i in "${array[@]}" #with quotes
do
echo $i
done
}
ar=("1. a" "2. b" "3. c")
print_array ${ar[@]}
When I execute the above script, the output is
1. a 2. b 3. c
Below is the code snippet without quotes around the array parameter:
#!/bin/bash
print_array ()
{
array=$@
for i in ${array[@]} #without quotes
do
echo $i
done
}
ar=("1. a" "2. b" "3. c")
print_array ${ar[@]}
When I execute the above script, the output is
1.
a
2.
b
3.
c
The output varies according to the quotes around the array parameter. I am really confused with the output displayed. Please help me out to resolve it.
The expected output should be :
1. a
2. b
3. c