Review your favorite Linux distribution.
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 08-11-2011, 02:43 AM   #1
Registered: Apr 2009
Posts: 74

Rep: Reputation: 16
Unhappy Where did the trailing newline characters go

I am working with bash scripting and running into a weird issue where the trailing newline characters are being truncated.

The file contents are something like:
# Catting the file shows the trailing \n characters
cat /tmp/junk
first line
second line
If I read it into a variable
myvar=`cat /tmp/junk`
The variable doesn't have the \n in the end
echo "$myvar"
first line
second line
Since I am working with config files, some of the applications/daemons fail to start if they fail to find trailing newline characters
Old 08-11-2011, 04:51 AM   #2
Registered: Mar 2006
Distribution: Funtoo
Posts: 124

Rep: Reputation: 26
Even without passing through a variable
echo "$(cat /tmp/junk)"
cuts the trailing newlines.

Last edited by EmaRsk; 08-11-2011 at 07:13 AM.
Old 08-11-2011, 10:36 AM   #3
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,577
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Bash command substitution removes trailing newlines
Old 08-11-2011, 12:09 PM   #4
LQ Newbie
Registered: Sep 2008
Posts: 6

Rep: Reputation: 2
myvar= && while read str; do myvar=$myvar$str"\n"; done < /tmp/junk
Old 08-13-2011, 12:56 AM   #5
Registered: Apr 2009
Posts: 74

Original Poster
Rep: Reputation: 16
Originally Posted by catkin View Post
Thanks for pointing me to this link
It has both the explanation and a workaround for my problem

Issue resolved


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
Sed remove trailing newline nc3b Programming 20 06-26-2015 12:02 PM
newline characters in folders Mirlyn Linux - General 3 09-03-2008 02:44 PM
Any tool to insert a newline each X characters in a file? neoAKiRAz Programming 5 05-09-2007 08:15 PM
Truncating trailing characters liguorir Programming 0 05-23-2004 05:00 AM
passwords with trailing characters murray_linux Slackware 4 04-08-2004 12:12 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:42 AM.

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