EN
Bash - concatenate two string variables
0 points
In this article, we would like to show you how to concatenate string variables in Bash.
Quick solution:
xxxxxxxxxx
1
2
3
variable_1='text_1'
4
variable_2='text_2'
5
6
result="${variable_1}${variable_2}"
In this example, we create a
and b
variables inside a script.sh
file and concatenate them into one - result
with space between them. At the end we display the result
using echo
command.
script.sh
file:
xxxxxxxxxx
1
2
3
a='Hello'
4
b='World!'
5
6
result="$a $b"
7
8
echo "$result"
Run in command line:
xxxxxxxxxx
1
./script.sh
Output:
xxxxxxxxxx
1
Hello World!
Note:
In this case, we don't need curly braces (
{}
). They are needed only when we want to limit variable name (explicitly indicate variable name).