简体   繁体   中英

Concatenating two string variables in bash appending newline

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?

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

It may depend on how you're trying to display the final result. Try outputting the resulting variable within double-quotes:

echo "$final_list"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM