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:
#!/bin/bash
variable_name='variable_value'
variable_name+=' additional_text'
result="${variable_1}"
Practical example
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:
#!/bin/bash
greeting='Hello'
greeting+=' World!'
echo "$greeting"
Run in command line:
./script.sh
Output:
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).