LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux 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

Reply
 
Search this Thread
Old 07-26-2009, 09:11 AM   #1
Coolmax
LQ Newbie
 
Registered: Jul 2009
Location: Poland
Distribution: openSUSE, Debian
Posts: 15

Rep: Reputation: 0
New line characters from command output in variable.


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
 
Old 07-26-2009, 09:56 AM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,488

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


Reply

Tags
bash, command, newline, output, substitution


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


All times are GMT -5. The time now is 01:37 PM.

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
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration