0

On ubuntu-14.04, bash-4.3.11, I run this:

$ _array=(1 2 3)
$ echo "${_array[@]} bloup"
1 2 3 bloup
$ printf "%s bloup\n" "${_array[@]}"
1 bloup
2 bloup
3 bloup
  • How can I make printf behave as echo in this situation?
  • Why does printf consider the double-quoted array as three arguments? i.e. what expansion mechanism am I missing here?

As a workaround, I use this:

$ _string=${_array[@]}
$ printf "%s bloup\n" "$_string"
1 2 3 bloup
1
  • 2
    If you use it double quoted with [*] it will expand to a single argument. printf "%s bloup\n" "${_array[*]}" Commented Sep 30, 2016 at 22:00

1 Answer 1

2

Just use * in place of @:

printf "%s bloup\n" "${_array[*]}"

The purpose of @ is to make treat the expansion as separate quoted words for each element, so that the expansion of an array like ("a b" c d) is treated as 3 arguments, not 4.

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

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.