LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-06-2011, 10:25 AM   #1
mystupidquestion
LQ Newbie
 
Registered: Oct 2011
Posts: 6

Rep: Reputation: Disabled
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
 
Old 10-06-2011, 10:56 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Try this:
Code:
echo "$blah"
Hope this helps.
 
Old 10-06-2011, 11:08 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script to remove first characters from every line (00) Linux - General 8 08-01-2011 10:28 AM
[SOLVED] Bash script to parse a file to get a set of line between a specific characters venkatrg Linux - Newbie 5 12-24-2010 06:55 AM
End-of-line Characters missing from last line of md5 file. Md5sum fails mehorter Linux - General 5 06-29-2009 08:56 PM
Removing new line characters on every line execpt first line bioinformatics_guy Linux - Newbie 4 10-21-2008 12:41 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:11 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration