Reputation: 65
I've an array of strings (variable size...) like this:
arr=( "one str" "another str" "example" "last-string" )
I need the following output:
one str:one str another str:another str example:example last-string:last-string
The problem is that when i do something like:
$(printf " %s:%s" "${arr[@]}")
It iterates over the array, moving to next position of the string (without repeating!), and the result is like:
one str:another str example:last-string
How can I achive this with printf? without any loops!
I'm using bash 3.1.0(1) in Cygwin if that helps!
Upvotes: 2
Views: 2514
Reputation: 2352
A bit hacky but you can try this:
arr=( "one str" "another str" "example" "last-string" )
intermediate_result=`printf " %s:%%s" "${arr[@]}"`
printf "$intermediate_result" "${arr[@]}"
Upvotes: 0
Reputation: 400512
You can't do this with bash's built-in printf
function. You'll either need to use a loop or an external program, e.g. Python:
# With a loop:
for x in "${arr[@]}"; do
printf " %s:%s" "$x" "$x"
done
# With Python
python -c 'import sys; print "".join(" %s:%s" % (arg, arg) for arg in sys.argv[1:])' "${arr[@]}"
Upvotes: 1