I have a variable final_list
which is appended by a variable url
in a loop as:
while read url; do final_list="$final_list"$'\n'"$url" done < file.txt
To my surprise the \n
is appended as an space, so the result is:
url1 url2 url3
while I wanted:
url1 url2 url3
What is wrong?
Printing Newline in Bash The most common way is to use the echo command. However, the printf command also works fine. Using the backslash character for newline “\n” is the conventional way. However, it's also possible to denote newlines using the “$” sign.
The += Operator in Bash Bash is a widely used shell in Linux, and it supports the '+=' operator to concatenate two variables. As the example above shows, in Bash, we can easily use the += operator to concatenate string variables.
In Windows, a new line is denoted using “\r\n”, sometimes called a Carriage Return and Line Feed, or CRLF. Adding a new line in Java is as simple as including “\n” , “\r”, or “\r\n” at the end of our string.
New lines are very much there in the variable "$final_list"
. echo
it like this with double quotes:
echo "$final_list" url1 url2 url3
OR better use printf
:
printf "%s\n" "$final_list" url1 url2 url3
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