1

given the following array:

arr=("hello hello" "bye bye")

How can I print any element from arr to stdout?

I tried to do it by the next way:

for line in ${arr[*]}; do
    echo ${line}
done

But in this state, the output is:
hello
hello
bye
bye

And I want to get:
hello hello
bye bye

2 Answers2

1

You just met shell word splitting.


If you quote (like you should), all is OK :

$ for line in "${arr[@]}"; do echo "$line"; done

Can be just (don't read lines with for):

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

Output :

hello hello
bye bye

"Double quote" every literal that contains spaces/metacharacters and every expansion: "$var", "$(command "$var")", "${array[@]}", "a & b". Use 'single quotes' for code or literal $'s: 'Costs $5 US', ssh host 'echo "$HOSTNAME"'. See
http://mywiki.wooledge.org/Quotes
http://mywiki.wooledge.org/Arguments
http://wiki.bash-hackers.org/syntax/words


The difference between $@ and $*: "$@" (quoted) expands to each positional parameter as its own argument: "$1" "$2" ... while "$*" expands to the single argument "$1c$2c..." where c is the first character of IFS. Unquoted $* and $@ are undefined; DO NOT use. You almost always want "$@". The same goes for arrays: "${array[@]}"

Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
0

I think it should read:

arr=("hello hello" "bye bye")
for line in "${arr[@]}" ; do
    echo $line
done

bash is trying to do it's best to interpret.

  • can you explain why with `${arr[@]}` it's not working? and what is the difference between `arr[@]` to `arr[*]` ? –  Feb 27 '18 at 21:13