Loosing trailing new line in Bash var assignment


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Loosing trailing new line in Bash var assignment
# 1  
Old 05-15-2014
Loosing trailing new line in Bash var assignment

I have come across a weird behaviour in bash and would love to get to the bottom of it. If I execute
Code:
echo -e "\na\nb\nc\n"

at the command line, I get:
Code:
a
b
c

However, if I wrap it in an assignment such as:
Code:
A="$( echo -e "\na\nb\nc\n" )"

then I get
Code:
a
b
c

It doesn't show very well, but it has no trailing new line. It is as if the assignment is loosing the trailing new line (everything after the 'c'). Does anyone know how to turn this off?
# 2  
Old 05-15-2014
Code:
A="\na\nb\nc\n"

Shows up as:
Code:
✔ ~ $ printf "$A"

a
b
c
+ ~ $

Or if using echo:
Code:
+ ~ $ echo -e "$A"

a
b
c

:) ~ $

That is, as echo always prints a 'newline' char at the end of a line automaticly, also with -e, eventhough, that is needed to supply more 'format commands'.
printf on the other hand, handles 'format commands' with ease, but it just prints everything 'on one line', thus, you need to append the tailing \n, where as with echo -e you had gotten an extra newline.

hth

Last edited by sea; 05-15-2014 at 10:37 AM..
# 3  
Old 05-15-2014
This has nothing to do with echo or print. It is how POSIX defines command substitution.
Quote:
The shell shall expand the command substitution by executing command in a subshell environment (see Shell Execution Environment) and replacing the command substitution (the text of command plus the enclosing "$()" or backquotes) with the standard output of the command, removing sequences of one or more <newline> characters at the end of the substitution.
To put it concisely, with command substitution, verbatim assignment of arbitrary text is impossible.

Regards,
Alister

Last edited by alister; 05-15-2014 at 11:36 AM..
# 4  
Old 05-15-2014
Think your right...

When I do

Code:
 A="$( echo -e "\nb\nc\nd\n" )"
set | grep ^A
A=$'\nb\nc\nd'

So, it does look as if the command substitution strips the whitespace at the end. Many thanks for clearing this up!!

Anyone with any idea of a way of negating this effect, I would love to hear from them!! Smilie
# 5  
Old 05-15-2014
There is none.

Perhaps if you provided us with the big picture, someone could suggest an alternative. What are you trying to accomplish?

Regards,
Alister
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash variable assignment failure/unary operator expected

I have a little code block (executing on AIX 7.1) that I cannot understand why the NOTFREE=0 does not appear to be assigned even though it goes through that block. This causes a unary operator issue. #!/bin/bash PLATFORM="AIX" NEEDSPC=3000 set -x if ; then lsvg | grep -v rootvg | while... (6 Replies)
Discussion started by: port43
6 Replies

2. Shell Programming and Scripting

Removing Trailing Line

I have been trying to remove empty lines and lines just filled with spaces. I have used the following command which does work. sed -i "/^\s*$/d" Except it leaves one single trailing line at the very end of the file. For the life of me I cant figure out why I cant remove that last trailing... (2 Replies)
Discussion started by: user8282892
2 Replies

3. Shell Programming and Scripting

Bash variable assignment question

Suppose I have a file named Stuff in the same directory as my script. Does the following assign the file Stuff to a variable? Var="Stuff" Why doesn't this just assign the string Stuff? Or rather how would I assign the string Stuff to a variable in this situation? Also, what exactly is... (3 Replies)
Discussion started by: Riker1204
3 Replies

4. Shell Programming and Scripting

Making a bash script and small C program for a homework assignment

Here's the assignment. I'll bold the parts that are rough for me. Unfortunately, that's quite a bit lol. The syntax is, of course, where my issues lie, for the most part. I don't have a lot of programming experience at all :/. I'd post what I've already done, but I'm so lost I really don't know... (1 Reply)
Discussion started by: twk101
1 Replies

5. Homework & Coursework Questions

Bash Shell Programming assignment.

Please take a look I am stuck on step 4 1. The problem statement, all variables and given/known data: #!/bin/bash ### ULI101 - ASSIGNMENT #2 (PART A) - DUE DATE Wed, Aug 3, 2011, before 12 midnight. ###==================================================================================== ###... (13 Replies)
Discussion started by: almirzaee
13 Replies

6. Shell Programming and Scripting

CPU assignment bash script

Hi guys, I'm basically looking for some help with a bash script I've written. It's purpose is to assign process to individual CPU cores once that process hits 15% CPU usage or more. If it drops below 15%, it's unassigned again (using taskset). My problem is that I can't think of a way to... (2 Replies)
Discussion started by: mcky
2 Replies

7. UNIX for Dummies Questions & Answers

I don't want to truncate trailing spaces and ^M at the end of line

I have a script wherein I access each line of the file using a FOR loop and then perform some operations in each line. The problem is each line that gets extracted in FOR loop truncates trailing blank spaces and control characters (^M) that is present at the end of each line. I don't wan this to... (5 Replies)
Discussion started by: Shobana_s
5 Replies

8. Shell Programming and Scripting

remove trailing spaces from a line

I want to remove the trailing spaces from any line of file. line ending does not follow any pattern. plz help (3 Replies)
Discussion started by: vikas_kesarwani
3 Replies

9. Shell Programming and Scripting

Trim trailing spaces from each line in a file

Hello folks, Is there a simple way to trim trailing spaces from each line a file. Please let me know. Regards, Tipsy. (5 Replies)
Discussion started by: tipsy
5 Replies
Login or Register to Ask a Question