324

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
jww
  • 97,681
  • 90
  • 411
  • 885
Axel Bregnsbo
  • 3,773
  • 2
  • 22
  • 16

8 Answers8

609

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!

F. Hauri - Give Up GitHub
  • 64,122
  • 17
  • 116
  • 137
Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
  • 6
    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. – danfuzz Mar 28 '13 at 20:59
93

Just quote the argument to echo:

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

the sub shell helps restoring the IFS after use

Tom Solid
  • 2,226
  • 1
  • 13
  • 32
perreal
  • 94,503
  • 21
  • 155
  • 181
  • 3
    sorry perreal, I moved my check mark to sputnick, despite liking your solution better, just because I learned about the 'printf' function. – Axel Bregnsbo Mar 28 '13 at 21:25
  • 3
    Thanks for this answer - I like it! Too bad assignments happen after expansion so `IFS=$'\n' echo "${my_array[*]}"` doesn't work. Oh well! – cxw Dec 14 '16 at 13:08
  • @cxw, what do you mean by "assignments happen"? – Steven Shaw Nov 22 '18 at 22:59
  • 1
    @bschlueter, I tried with Bash 4 — 4.4.23(1)-release — and it works! – Steven Shaw Nov 22 '18 at 23:00
  • @StevenShaw [this](https://www.gnu.org/software/bash/manual/html_node/Simple-Command-Expansion.html) - in my example, bash saves the `IFS=...` (linked #1), then expands `"${my_array[*]}"` (#2), _then_ changes `IFS` (#4). As a result, you can't do this in one command - you need the IFS change first, so you need a subshell `( ... )` unless you are willing to change IFS in your current shell. – cxw Nov 22 '18 at 23:12
  • 1
    @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! https://gist.github.com/steshaw/53ba0095bce8ccab52d26a14375dedb8 – Steven Shaw Nov 24 '18 at 02:05
  • 1
    I do like the `printf` solution though. I hadn't realised that, as the Bash manual points out, "The format is reused as necessary to consume all of the arguments". It's a bit counterintuitive for C programmers, however . – Steven Shaw Nov 24 '18 at 02:12
  • @StevenShaw good idea - I hadn't thought of using a function. It's not because `echo` is a builtin, though. It's because the function in your gist includes the expansion, so the expansion doesn't happen until after the function is called, by which point `IFS` has already been changed. – cxw Nov 24 '18 at 12:57
  • 1
    @cxw, Bash is a bit nasty, isn't it? – Steven Shaw Nov 24 '18 at 22:03
61

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[@]}"
Zombo
  • 1
  • 62
  • 391
  • 407
7

Another useful variant is pipe to tr:

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

This looks simple and compact

Steven Shaw
  • 6,063
  • 3
  • 33
  • 44
0x00
  • 187
  • 1
  • 5
  • 17
    Except this breaks on `my_array=( "one two" three )` – Mike Holt Oct 18 '17 at 20:34
  • Fixed it with double quotes. – Steven Shaw Nov 22 '18 at 22:52
  • 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. – S0AndS0 Apr 25 '19 at 23:46
4

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
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.

wuxmedia
  • 449
  • 4
  • 6
2

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

nicolas
  • 9,549
  • 3
  • 39
  • 83
1

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

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