EN
Bash - add text to string variable
0 points
In this article, we would like to show you how to add text to string variable in Bash.
Quick solution:
xxxxxxxxxx
1
2
3
variable_name='variable_value'
4
variable_name+=' additional_text'
5
6
result="${variable_1}"
In this example, we create greeting
variable inside a script.sh
file and add text to it using +=
operator. At the end we display the variable using echo
command.
script.sh
file:
xxxxxxxxxx
1
2
3
greeting='Hello'
4
greeting+=' World!'
5
6
echo "$greeting"
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).