I have a variable called deps:
deps='word1 word2'
I want to add a prefix to each word of the variable.
I tried with:
echo $deps | while read word do \ echo "prefix-$word" \ done
but i get:
bash: syntax error near unexpected token `done'
any help? thanks
For well behaved strings, the best answer is:
printf "prefix-%s\n" $deps
as suggested by 123 in the comments to fedorqui's answer.
Explanation:
$deps
according to $IFS
(which defaults to " \n\t"
) before calling printf
printf
evaluates the pattern for each of the provided arguments and writes the output to stdout.printf
is a shell built-in (at least for bash) and does not fork another process, so this is faster than sed-based solutions.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With