I have
var="a b c"
for i in $var
do
p=`echo -e $p'\n'$i`
done
echo $p
I want the last echo to print:
a
b
c
Notice that I want the variable p to contain newlines. How do I do that?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Summary
Inserting a new line in the source code
Using
$'\n'(only Bash and Z shell)Using
echo -eto convert\nto a new lineDetails
Inserting a new line in the source code
Avoid extra leading newline
Using a function
Using
$'\n'(less portable)bash and zsh interprets
$'\n'as a new line.Avoid extra leading newline
Using a function
Using
echo -eto convert\nto a new lineecho -einterprets the two characters"\n"as a new line.Avoid extra leading newline
Using a function
⚠ Inserting
"\n"in a string is not enough to insert a new line:"\n"are just two characters.The output is the same for all
Special thanks to contributors of this answer: kevinf, Gordon Davisson, l0b0, Dolda2000 and tripleee.
forloop in the above Bash snippets.