0

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
1
  • You can't copy an array into a scalar -- a scalar can hold a single array element only, but not more than that without losing data. Commented Nov 14, 2014 at 13:47

2 Answers 2

2
#!/bin/bash

print_array ()
{
        for i;
        do
                printf "%s\n" "$i"
        done
}

ar=("1. a" "2. b" "3. c")
print_array "${ar[@]}"  # with quotes

If you want to be explict, you can write for i in "$@"

You can also write:

#!/bin/bash

print_array ()
{
        array=("$@")
        for i in "${array[@]}"; do
                printf "%s\n" "$i"
        done
}

ar=("1. a" "2. b" "3. c")
print_array "${ar[@]}"  # with quotes
Sign up to request clarification or add additional context in comments.

15 Comments

IOW, $@ is a string, ("$@") is an array. ($@) is also an array, but with a different number of elements.
@sach, using the same name array both inside and outside the function would actually be a bad idea, since by default (without an explicit local declaration), variables inside function definitions are actually global in scope.
@WilliamPursell, why are the quotes so mandatory to get the desired output.
@sach, if you don't quote you get string-splitting and glob-expansion, so individual words inside the string (as distinguished by splitting on characters in IFS) are passed as individual arguments. For extra fun, try passing the literal asterisk character surrounded by one of your spaces within an array element, and you'll see what glob expansion does when you don't quote also.
@sach, I don't strongly recommend any dead-tree reference, but the documentation at mywiki.wooledge.org/BashGuide (also the FAQ, the pitfalls page, etc) is a good choice (actively maintained by folks who care deeply about correctness and showcasing good practices).
|
0
#!/bin/bash

print_array ()
{
    array=("$@")
    for i in "${array[@]}"
    do
            echo "$i"
    done
}

ar=("1. a" "2. b" "3. c")
print_array "${ar[@]}"

Result:

1. a
2. b
3. c

Using =() during assignment keeps the variable as an array.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.