LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Linux > Linux - Newbie
User Name
Password
Linux - Newbie This forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Tags used in this thread
Popular LQ Tags , , , ,

Reply
 
Thread Tools
Old 07-26-2009, 10:11 AM   #1
Coolmax
LQ Newbie
 
Registered: Jul 2009
Posts: 7
Thanked: 0
New line characters from command output in variable.


[Log in to get rid of this advertisement]
Hi,
in man bash, section "Command Substitution" I read that new lines are removed from output:

Quote:
Command Substitution
Command substitution allows the output of a command to replace the command name. There are two forms:


$(command)
or
`command`

Bash performs the expansion by executing command and replacing the command substitution with the standard
output of the command, with any trailing newlines deleted. Embedded newlines are not deleted, but they
may be removed during word splitting. (...)
Is there any way to avoid this situation and set output to variable "as is"?

--
Best regards,
Matthew
Coolmax is offline  
Tag This Post , , , ,
Reply With Quote
Old 07-26-2009, 10:56 AM   #2
colucix
Guru
 
Registered: Sep 2003
Location: Bologna, Italia
Distribution: OpenSUSE 11.1 CentOS 5.4 VectorLinux 6.0
Posts: 5,129
Thanked: 464
The only way I know is to add an extra line and remove it later. In this way the trailing newlines are preserved since they become embedded, then you can strip the added chars using parameter substitution. This will demonstrate:
Code:
#!/bin/bash
var=$(cat testfile; echo c)
var=${var%??}
echo -n "lines in variable are: "
echo "$var" | wc -l
Here is the output:
Code:
$ cat testfile
line 1
line 2
line 3
line 4


$ wc -l testfile
6 testfile
$ ./test.sh
lines in variable are: 6
The double ?? matches the added "\n" and "c".
colucix is offline     Reply With Quote

Reply

Bookmarks


Thread Tools

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
how to read certain line of file, and output to variable? jimmyjiang Red Hat 2 05-01-2007 09:46 AM
Bash store last line from displayed text output in a variable carl0ski Programming 1 01-16-2007 04:38 AM
Strange characters in command line in X Vitalie Ciubotaru Linux From Scratch 5 11-30-2006 07:53 PM
Getting last characters of a line with sed command LULUSNATCH Programming 4 12-21-2005 10:33 AM
51 characters only in the 1st Line of command line eggCover Linux - General 2 07-29-2004 02:28 PM


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

Main Menu
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
RSS2  LQ Podcast
RSS2  LQ Radio
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration