Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


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

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

in man bash, section "Command Substitution" I read that new lines are removed from output:

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


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

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
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:
var=$(cat testfile; echo c)
echo -n "lines in variable are: "
echo "$var" | wc -l
Here is the output:
$ cat testfile
line 1
line 2
line 3
line 4

$ wc -l testfile
6 testfile
$ ./
lines in variable are: 6
The double ?? matches the added "\n" and "c".


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 > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:20 PM.

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