How to concatenate string variables in Bash

Strawberry picture Strawberry · Nov 15, 2010 · Viewed 3.9M times · Source

In PHP, strings are concatenated together as follows:

$foo = "Hello";
$foo .= " World";

Here, $foo becomes "Hello World".

How is this accomplished in Bash?

Answer

codaddict picture codaddict · Nov 15, 2010
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