
On 2011-11-11 17:38, Andrew Spiers wrote:
I couldn't google for this. How do I preserve line breaks when assigning the result of something to a variable?
aspiers@cape:~/tmp/horror$ ls -1 file1 file2 file3 aspiers@cape:~/tmp/horror$ echo -e $(ls -1) file1 file2 file3
There must be something better than this:
aspiers@cape:~/tmp/horror$ echo -e $(ls -1| awk '{print $0,"\\n"}') file1 file2 file3
Parsing ls output is *bad*! mattcen@tony:tmp$ touch a b c "file with spaces" mattcen@tony:tmp$ ls a b c file with spaces typescript mattcen@tony:tmp$ files=$(for file in *; do printf "%s\n" "$file"; done) mattcen@tony:tmp$ echo $files a b c file with spaces typescript mattcen@tony:tmp$ echo "$files" a b c file with spaces typescript mattcen@tony:tmp$ Note that the former "echo" doesn't have quotes around the variable name, and therefore doesn't honour the newlines. -- Regards, Matthew Cengia