bash variable store and print multiple lines with new line

Bash supports string multi line output in a variable, also it allows to print multi line output with new line.

To store multi line output in a variable:

VAR=$(ls)

Above command will store ls outputs in VAR.

In general we will use echo to print content of variable. Here I will use use echo in two different forms and observe the output.

echo command without new line:

$ echo $VAR

Output:

t tmp tmp1 t.py

echo command with new line:

$ echo "$VAR"

t
tmp
tmp1
t.py

Just observe difference between above two commands, echo without double quotes given output without new lines and echo with double quotes given output with new line.  So whenever you store a multi line text in a variable use with double quotes.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s