Reputation: 65
var = "a common string \n $var"
I am passing this var to some other method and I am printing var in that other method.i am not able to edit that other method. So echo -e
and printf
statements cannot be used by me. But I need that \n to be printed as new line instead of that exact literal.
Upvotes: 4
Views: 6928
Reputation: 1521
If you are setting var
in shell script you can actually use printf
as follows:
var=$(printf "a common string\n%s" "$var")
or, in newer bash
printf -v var "a common string\n%s" "$var"
If your shell supports the $'…'
construct, you can do this instead:
var=$'a common string \n'"$var"
Upvotes: 4
Reputation: 6458
Treat the variable as a list with '\n' delimiter so you can go over it in a loop and print each item on it in a new line, something like:
for item in $var; do
echo $item
done
Upvotes: 0