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.
By default,
bash
does not process escape characters. The assignmentassigns 8 characters to the variable
VAR
: 'f', 'o', 'o', '\', 'n', 'b', 'a', and 'r'. The POSIX standard states that theecho
command should treat the two-character string\n
in its arguments as a linefeed;bash
does not follow the standard in this case and requires the-e
option to enable this processing. Theprintf
command follows the POSIX specification and treats literal "\n" in its argument as a linefeed, but does not expand such uses in strings that replace placeholders:printf "%s\n" "$VAR"
would still outputinstead of
To include an actual linefeed character in a string, you can use ANSI quoting:
which has the drawback that the string is otherwise processed as a single-quoted string, and cannot contain parameter expansions or command substitutions. Another option is that a string that spans multiple lines will contain the quoted linefeed characters:
It works this way:
Using ANSI-C quoting:
You could put the
$'\n'
in a variable:By the way, in this case, it's better to use the concatenation operator:
If you don't like ANSI-C quoting, you can use
printf
with its-v
option:Then, to print the content of the variable
var
, don't forget quotes!or, better yet,
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:
Then:
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:Look how well it works:
It's a funny trickery with
IFS
and"$*"
.