Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Build a string in bash with newlines

I'm going through a script in Bash where, depending on the conditionals, I want to append to a variable different things and then display it at the very end. Something like this:

VAR="The "

if [[ whatever ]]; then
    VAR="$VAR cat wears a mask"
elif [[ whatevs ]]; then
    VAR="$VAR rat has a flask"
fi

But I run into difficulties if I try to use this form of building up VAR by appending to it when I want to occasionally append newlines into it. How would I do VAR="$VAR\nin a box", for example?

I have seen usage of $'\n' before, but not when also trying to use $VAR because of the appending.

like image 382
Palace Chan Avatar asked Nov 11 '13 17:11

Palace Chan


People also ask

How do I add a line break to a string in bash?

Printing Newline in Bash Using the backslash character for newline “\n” is the conventional way. However, it's also possible to denote newlines using the “$” sign.

How do you declare a string in bash?

To initialize a string, you directly start with the name of the variable followed by the assignment operator(=) and the actual value of the string enclosed in single or double quotes. Output: This simple example initializes a string and prints the value using the “$” operator.

How do you add a newline to a string?

Adding Newline Characters in a String In Windows, a new line is denoted using “\r\n”, sometimes called a Carriage Return and Line Feed, or CRLF. Adding a new line in Java is as simple as including “\n” , “\r”, or “\r\n” at the end of our string.

What is $@ in bash?

bash [filename] runs the commands saved in a file. $@ refers to all of a shell script's command-line arguments. $1 , $2 , etc., refer to the first command-line argument, the second command-line argument, etc. Place variables in quotes if the values might have spaces in them.


1 Answers

Using ANSI C quoting:

var="$var"$'\n'"in a box"

You could put the $'\n' in a variable:

newline=$'\n'
var="$var${newline}in a box"

By the way, in this case, it's better to use the concatenation operator:

var+="${newline}in a box"

If you don't like ANSI C quoting, you can use printf with its -v option:

printf -v var '%s\n%s' "$var" "in a box"

Then, to print the content of the variable var, don't forget quotes!

echo "$var"

or, better yet,

printf '%s\n' "$var"

Remark. Don't use upper case variable names in Bash. It's terrible, and one day it will clash with an already existing variable!


You could also make a function to append a newline and a string to a variable using indirect expansion (have a look in the Shell Parameter Expansion section of the manual) as so:

append_with_newline() { printf -v "$1" '%s\n%s' "${!1}" "$2"; }

Then:

$ var="The "
$ var+="cat wears a mask"
$ append_with_newline var "in a box"
$ printf '%s\n' "$var"
The cat wears a mask
in a box
$ # There's no cheating. Look at the content of 'var':
$ declare -p var
declare -- var="The cat wears a mask
in a box"

Just for fun, here's a generalized version of the append_with_newline function that takes n+1 arguments (n≥1) and that will concatenate them all (with exception of the first one being the name of a variable that will be expanded) using a newline as separator, and puts the answer in the variable, the name of which is given in the first argument:

concatenate_with_newlines() { local IFS=$'\n'; printf -v "$1" '%s\n%s' "${!1}" "${*:2}"; }

Look how well it works:

$ var="hello"
$ concatenate_with_newlines var "a gorilla" "a banana" "and foobar"
$ printf '%s\n' "$var"
hello
a gorilla
a banana
and foobar
$ # :)

It's a funny trickery with IFS and "$*".

like image 66
gniourf_gniourf Avatar answered Sep 22 '22 02:09

gniourf_gniourf