Concatenate two variable into new one bash
A simple one!
In Bash, you can concatenate two variables into a new one using the following syntax:
new_variable=${variable1}${variable2}
For example:
foo="Hello"
bar="World"
new_variable=${foo}${bar}
echo $new_variable
# Output: HelloWorld
Alternatively, you can use the +=
operator to concatenate the variables:
new_variable=${foo}${bar}
Or, you can use the printf
command:
new_variable=$(printf "%s%s" "$foo" "$bar")
Note that in the last example, the $( )
syntax is used to capture the output of the printf
command as a string.