m-ric
m-ric

Reputation: 5901

make bash printf consider array as one argument

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

As a workaround, I use this:

$ _string=${_array[@]}
$ printf "%s bloup\n" "$_string"
1 2 3 bloup

Upvotes: 0

Views: 51

Answers (1)

chepner
chepner

Reputation: 531948

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.

Upvotes: 2

Related Questions