Bash String Concatenation

ww‮.w‬lautturi.com
Bash String Concatenation

In Bash, you can concatenate (join) two or more strings together using the + operator. For example, if you have two strings $str1 and $str2, you can concatenate them like this:

str1="Hello"
str2="World"

concatenated_string="$str1 $str2"

This will set the concatenated_string variable to "Hello World", which is the concatenation of $str1 and $str2 with a space in between.

Alternatively, you can use the += operator to concatenate strings in Bash. This operator appends the right-hand side string to the end of the left-hand side string. For example:

str1="Hello"
str2="World"

str1+=" $str2"

This will set the str1 variable to "Hello World", which is the concatenation of $str1 and $str2 with a space in between.

Both of these methods can be used to concatenate strings in Bash. The choice of which method to use depends on your specific situation and preference.

Created Time:2017-10-16 14:38:39  Author:lautturi