BaRud
BaRud

Reputation: 3228

repeating a printf format in bash

I am trying to printf an array in bash, whose number of element is previously unknown. so, the goal is to have something like:

printf  "latc= ${#latc[@]}%s\n" ${latc[@]}

but it does not seem to be possible. I even tried solution of this thread as

for x in "${latc[@]}"
do
  printf " %s:%s\n" ${x}
done

, but I am not getting what I want.

currently I am working with:

printf  "       latc="
echo ${latc[@]}

clearly not an elegant method of doing thigs. Any help please?

Upvotes: 0

Views: 398

Answers (2)

chepner
chepner

Reputation: 531948

It looks like you want

printf "latc=%s\n" "${latc[*]}"

Quoting ${latc[*]} produces a single string in which the elements of latc are joined using the first character of IFS (by default, a space). Quoting ${latc[@]} induces a special expansion that produces one word for each element of the array. There is no way to indicate a repeated placeholder in the format string as you seemed to be trying with ${#latc[*]}%s.

Upvotes: 1

anubhava
anubhava

Reputation: 785691

You can print array as:

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

Upvotes: 0

Related Questions