LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   new line characters not preserved in bash (https://www.linuxquestions.org/questions/programming-9/new-line-characters-not-preserved-in-bash-906757/)

mystupidquestion 10-06-2011 10:25 AM

new line characters not preserved in bash
 
I have a bash scripting question. When I assign the output of a command to a variable:

export blah="$(ls -l)"

Then print the contents of blah:
echo $blah

the new line characters in ls -l's output is not preserved.

otal 106 dr-xr-xr-x. 2 root root 4096 Oct 5 03:18 bin dr-xr-xr-x. 5 root root 1024 Aug 24 09:12 boot -rw-r--r--. 1 root root 215 Aug 25 09:21 chart.txt drwxr-xr-x. 20 root root 4200 Oct 6 08:10 dev drwxr-xr-x. 127

I need the command's columns to remain in tact, a new line after each row ends. How can I save the newline characters in a command's output?

Thanks.

Thanks

druuna 10-06-2011 10:56 AM

Hi,

Try this:
Code:

echo "$blah"
Hope this helps.

David the H. 10-06-2011 11:08 AM

It's vital in scripting to understand how the shell handles arguments and whitespace. Read through these three links:

http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes

And please use [code][/code] tags around your code, to preserve formatting and to improve readability.


All times are GMT -5. The time now is 08:17 PM.