371

How do I print the array element of a Bash array on separate lines? This one works, but surely there is a better way:

$ my_array=(one two three)
$ for i in ${my_array[@]}; do echo $i; done
one
two
three

Tried this one but it did not work:

$ IFS=$'\n' echo ${my_array[*]}
one two three
1
  • 3
    in zsh print -l $path Commented Jan 29, 2022 at 10:07

8 Answers 8

678

Try doing this :

$ printf '%s\n' "${my_array[@]}"

The difference between $@ and $*:

  • Unquoted, the results are unspecified. In Bash, both expand to separate args and then wordsplit and globbed.

  • Quoted, "$@" expands each element as a separate argument, while "$*" expands to the args merged into one argument: "$1c$2c..." (where c is the first char of IFS).

You almost always want "$@". Same goes for "${arr[@]}".

Always double quote them!

Sign up to request clarification or add additional context in comments.

3 Comments

And note, the double quotes around the variable reference are important if you want to make sure elements with internal spaces aren't inadvertently split up.
Consider this question.
Just up-voted this because it was at 666 up-votes.
107

Just quote the argument to echo:

(IFS=$'\n'; echo "${my_array[*]}")

the sub shell helps restoring the IFS after use

10 Comments

sorry perreal, I moved my check mark to sputnick, despite liking your solution better, just because I learned about the 'printf' function.
Thanks for this answer - I like it! Too bad assignments happen after expansion so IFS=$'\n' echo "${my_array[*]}" doesn't work. Oh well!
@bschlueter, I tried with Bash 4 — 4.4.23(1)-release — and it works!
@cxw Ah, I didn't see what you were trying to do there. I think it doesn't work because echo is a builtin in Bash. However, you can wrap it in a function and it will work! gist.github.com/steshaw/53ba0095bce8ccab52d26a14375dedb8
@cxw, Bash is a bit nasty, isn't it?
|
65

Using for:

for each in "${alpha[@]}"
do
  echo "$each"
done

Using history; note this will fail if your values contain !:

history -p "${alpha[@]}"

Using basename; note this will fail if your values contain /:

basename -a "${alpha[@]}"

Using shuf; note that results might not come out in order:

shuf -e "${alpha[@]}"

3 Comments

"shuf"... "might not come out in order"... hilarious.
for shuf. Who would have thought to use that?
histchars= history -p "${alpha[@]}" should work all the time
9

Another useful variant is pipe to tr:

echo "${my_array[@]}" | tr ' ' '\n'

This looks simple and compact

3 Comments

Except this breaks on my_array=( "one two" three )
Fixed it with double quotes.
Didn't work as advertised on Bash version 4+, had to use echo "${my_array[@]}" | tr '' ' \n', though personally I'd avoid using echo like that, where tr my choice I think something like tr '' ' \n' <<<"${my_array[@]}" might be a bit easier to read later.
5

You could use a Bash C Style For Loop to do what you want.

my_array=(one two three)

for ((i=0; i < ${#my_array[@]}; i++ )); do echo "${my_array[$i]}"; done
one
two
three

Comments

3

I tried the answers here in a giant for...if loop, but didn't get any joy - so I did it like this, maybe messy but did the job:

 # EXP_LIST2 is iterated    
 # imagine a for loop
     EXP_LIST="List item"    
     EXP_LIST2="$EXP_LIST2 \n $EXP_LIST"
 done 
 echo -e $EXP_LIST2

although that added a space to the list, which is fine - I wanted it indented a bit. Also presume the "\n" could be printed in the original $EP_LIST.

1 Comment

Doesn't look like a complete example.
3

In zsh one can use the built-in command print -l $path

1 Comment

You can also join the array elements with newlines: echo ${(j:\n:)path}
1

I've discovered that you can use eval to avoid using a subshell. Thus:

IFS=$'\n' eval 'echo "${my_array[*]}"'

2 Comments

Never ever use the evil eval
That is just nonsense. Just never use evil eval on user input.

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.