34

I have a variable in a shell script in which I'd like to format the data. The variable stores new data during every iteration of a loop. Each time the new data is stored, I'd like to insert a new line character. Here is how I'm trying to store the data into the variable.

VARIABLE="$VARIABLE '\n' SomeData"

Unfortunately, the output includes the literal '\n' Any help would be appreciative.

9 Answers 9

59

Try $'\n':

VAR=a
VAR="$VAR"$'\n'b
echo "$VAR"

gives me

a
b
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks vmpstr. I may be mistaken, but I believe VAR=$VAR$'\n'b must be surrounded by double quotes. Which results in a literal '\n'.
You can switch between different kinds of quotes in the same "word": VAR="$VAR"$'\n'"some data" has $VAR in double-quotes, \n in $'' so it'll get interpreted as a newline, and then some data back in double-quotes. As long as there's no space between them, they'll all be concatenated together and the result assigned to VAR.
Thanks, I added double quotes around $VAR so that the existing newlines are preserved
The output of echo is different than how the values are being stored in VARIABLE. The contents of VARIABLE are passed to an email util. I am not echoing the data to command line. Sorry for the confusion.
I got it! Turns out I had some spacing issues. Thanks to everyone who cared to comment, especially Gordon Davisson. Here was my issue: Original Code: VAR="$VAR"$'\n'"Audit Source: $B Audit Path: $C" The spaces for formatting was messing things up. New Code:VAR="$VAR"$'\n'"Audit Source:$B"$'\t'"Audit Path:$C" No spaces here! Anyone know why the spacing causes issues? Even though the Original Code has everything surrounded by "?
|
21

A common technique is:

nl='
'
VARIABLE="PreviousData"
VARIABLE="$VARIABLE${nl}SomeData"

echo "$VARIABLE"
PreviousData
SomeData

Also common, to prevent inadvertently having your string start with a newline:

VARIABLE="$VARIABLE${VARIABLE:+$nl}SomeData"

(The expression ${VARIABLE:+$nl} will expand to a newline if and only if VARIABLE is set and non-empty.)

7 Comments

Why the downvote? This is absolutely the canonical way to inject newlines into a variable.
The reason is is common is likely because people don't know about $'\n'. edit: I had originally downvoted but decided to just stick with a comment instead.
@jordanm Or more likely because people are aware that \n only works in a limited number of shells, while this technique will work in all shells.
Recommend enclosing the variables in quotes. While bash may be OK, most shells will not be OK.
Doesn't the entire value need to be double quoted in order to append SomeData to VARIABLES Such as VAR="$VARIABLE${nl}SomeData" In this answer, there are two separate variables (VAR and VARIABLE I'm trying to append VARIABLE to VARIABLE
|
6
VAR="one"
VAR="$VAR.\n.two"
echo -e $VAR

Output:

one.
.two

3 Comments

Thanks Alex. I think when you echo here, the output is on multiple lines. But I think the variable is still be stored on one line? I'm not sure.
You're correct, -e makes echo treat escaped characters as special characters, without -e you would see entire value on one line with "\n" in the middle
Unfortunately I'm storing data in VAR and then sending the contents of VAR to an email utility. So the value can't be echoed, it must be formatted within the var itself. Sorry for the confusion.
4

Other than $'\n' you can use printf also like this:

VARIABLE="Foo Bar"
VARIABLE=$(printf "${VARIABLE}\nSomeData")
echo "$VARIABLE"

OUTPUT:

Foo Bar
SomeData

1 Comment

Thank you anubhava. But like the answer below, the output of echo is different than how the values are being stored in VARIABLE. The contents of VARIABLE are passed to an email util. I am not echoing the data to command line. Sorry for the confusion.
2

I had a problem with all the other solutions: when using a # followed by SPACE (quite common when writing in Markdown) both would get split onto a new line.

So, another way of doing it would involve using single quotes so that the "\n" get rendered.

FOO=$'# Markdown Title #\n'
BAR=$'Be *brave* and **bold**.'
FOOBAR="$FOO$BAR"

echo "$FOOBAR"

Output:

# Markdown Title #
Be *brave* and **bold**.

Comments

2

Single quote All special characters between these quotes lose their special meaning.
https://www.tutorialspoint.com/unix/unix-quoting-mechanisms.htm

So the syntax you use does something different that you want to achieve.

This is what you need:

The $'\X' construct makes the -e option in echo unnecessary.
https://linux.die.net/abs-guide/escapingsection.html

echo -e "something\nsomething"

or

echo "something"$'\n'"something"

Comments

2

It's a lot simpler than you think:

VARIABLE="$VARIABLE
SomeData"

Comments

1

Building upon the first two solutions, I'd do like shown below. Concatenating strings with the '+=' operator, somehow looks clearer to me.

Also rememeber to use printf as opposed to echo, you will save yourself so much trouble

sometext="This is the first line"
sometext+=$'\n\n'
sometext+="This is the second line AFTER the inserted new lines"
printf '%s' "${sometext}"

Outputs:

This is the first line

This is the third line AFTER the inserted new line

Comments

0

Your problem is in the echo command, in ash you have to use the option -e to expand special characters. This should work for you:

VAR="First line"
VAR="$VAR\nSecond line"
echo -e $VAR

This outputs

First line
Second line

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.