Today I Learned

hashrocket A Hashrocket project

String concatentation in the Bourne Again Shell

String concatenation must be different in every lanaguage, or so it seems, and Bash is no different! You can string concat just by putting too strings next to each other.

> echo "a""b"
ab 
Bash

Its ok if one of these strings is a variable.

> a = "x" 
> echo $a"b"
xb
Bash

Its ok if you put a double quoted string next to a single quoted string.

> echo 'a'"b"
ab
Bash

Its ok if you put spaces in between the strings, but that space now will be part of the new string.

> echo "a" "b"
a b
Bash

But multiple spaces in between strings will be squashed to one string.

> echo "a"      "b"
a b
Bash

This works when setting a variable too, but only surrounded by parens.

> c=('a'      'b')
> echo $c
a b
Bash

Don't worry about the parens though when they are right next to each other.

> c = "a""b"
> echo $c
ab
Bash
See More #command-line TILs