Concatenating Two String Variables in Bash Appending Newline

Concatenating two string variables in bash appending newline

New lines are very much there in the variable "$final_list". echo it like this with double quotes:

echo "$final_list"
url1
url2
url3

OR better use printf:

printf "%s\n" "$final_list"
url1
url2
url3

How concatenate strings with a newline

Use $'...' to have the shell interpret escape sequences.

enter=$'\n'
lines=$line1$enter$line2

You can also put a newline directly inside double quotes:

lines="$line1
$line2"

Or use printf:

printf -v lines '%s\n%s' "$line1" "$line2"

Concatenate strings with newline in bash?

Add a newline, not two characters \ and n, to the string.

mystr=""
mystr+="First line here"$'\n'
mystr+="Second line here"$'\n'
mystr+="Third line here"$'\n'
echo "$mystr"

Or you can interpret \ escape sequences - with sed, with echo -e or with printf "%b" "$mystr".

Concatenating multiple line variables using newline

Using bash, you can use this function for this job:

concat() {
printf '%s' "${1:+$1$'\n'}" "${2:+$2$'\n'}";
}

Use of "${1:+$1$'\n'}" appends \n to $1 only if $1 is null/unset.

As per man bash:

${parameter:+word}
Use Alternate Value. If parameter is null or unset, nothing is substituted, otherwise the expansion of word is substituted.

Examples:

concat 'line1' 'line2'

line1
line2

concat 'line1' ''

line1

concat '' 'line2'

line2

concat '' ''

concat 'line1
line2
line3' ''

line1
line2
line3

Build a string in Bash with newlines

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 "$*".

Concatenate in bash the output of two commands without newline character

You can use tr:

{ echo "The quick"; echo "brown fox"; } | tr "\n" " "

OR using sed:

{ echo "The quick"; echo "brown fox"; } | sed ':a;N;s/\n/ /;ba'

OUTPUT:

The quick brown fox 

How to append new line character in bash script if concatenating using +=?

You can include the option in your echo:

echo -e $DATA >> $FILE

-e enables echo to interpret backslash escapes.

How to concatenate string variables in Bash

foo="Hello"
foo="${foo} World"
echo "${foo}"
> Hello World

In general to concatenate two variables you can just write them one after another:

a='Hello'
b='World'
c="${a} ${b}"
echo "${c}"
> Hello World


Related Topics



Leave a reply



Submit