306

I’m using bash shell on Linux. I have this simple script ...

#!/bin/bash
TEMP=`sed -n '/'"Starting deployment of"'/,/'"Failed to start context"'/p' "/usr/java/jboss/standalone/log/server.log" | tac | awk '/'"Starting deployment of"'/ {print;exit} 1' | tac`
echo $TEMP

However, when I run this script

./temp.sh

all the output is printed without the carriage returns/new lines. Not sure if its the way I’m storing the output to $TEMP, or the echo command itself.

How do I store the output of the command to a variable and preserve the line breaks/carriage returns?

ivanleoncz
10.3k7 gold badges62 silver badges53 bronze badges
asked Feb 28, 2014 at 17:22
2
  • 11
    They're preserved in the variable just fine (except for the last one); it's your echo that's broken. See mywiki.wooledge.org/BashPitfalls entry #14 Commented Feb 28, 2014 at 17:32
  • 1
    also, don't store commands in variables. Use a function instead Commented Aug 26, 2019 at 7:01

2 Answers 2

573

With shell scripting, one needs to always quote variables, especially when working with strings.

Here is an example of the problem:

Example variable:

$ f="fafafda
> adffd
> adfadf
> adfafd
> afd"

Output without quoting the variable:

$ echo $f
fafafda adffd adfadf adfafd afd

Output WITH quoting the variable:

$ echo "$f"
fafafda
adffd
adfadf
adfafd
afd

Explaination:

Without quotes, the shell replaces $TEMP with the characters it contains (one of which is a newline). Then, before invoking echo shell splits that string into multiple arguments using the Internal Field Separator (IFS), and passes that resulting list of arguments to echo. By default, the IFS is set to whitespace (spaces, tabs, and newlines), so the shell chops your $TEMP string into arguments and it never gets to see the newline, because the shell considers it a separator, just like a space.

Mike Q
7,5055 gold badges62 silver badges69 bronze badges
answered Feb 28, 2014 at 17:25
Sign up to request clarification or add additional context in comments.

3 Comments

xdotool type "$myVar" doesn't work. it's still typing the content of the variable without new lines
What if you want to do something like FOO="$(echo $VAR)"; how do you quote $VAR properly when it's already inside quotes?
@weberc2 You simply quote it again: "$(echo "$VAR")". Yes, it's odd to parse, but apparently easier for shells than for us.
7

I have ran into the same problem, a quote will help

ubuntu@host:~/apps$ apps="abc
> def"
ubuntu@host:~/apps$ echo $apps
abc def
ubuntu@host:~/apps$ echo "$apps"
abc
def
answered Feb 13, 2019 at 19:08

2 Comments

Answering a question almost 5 years later without adding any new details to the previous answer(s) or providing a new solution is not a good idea; please avoid posting these kinds of answers.
Loving the precision of your "almost 5 years", @MAChitgarha, :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.