Bash

Bash String Concatenation

Bash String Concatenation

In this topic, we've explained the way to add or concatenate strings in Bash Shell Scripting.

In bash scripting, we will add or join two or more strings together, which is understood as string concatenation. it's one of the common requirements for any programing language. A special character or built-in function is utilised to complete string concatenation. However, Bash doesn't contain any built-in function to mix string data or variables. the simplest method to perform string concatenation in bash is to write down variables side by side.

For example, assume that we've two strings (i.e., "welcome" & "to Great learning academy"), and that we join both the strings together and a replacement string ("welcome to Great learning academy") is made. this idea is mentioned as String Concatenation.

Command

The example command for concatenating the strings are often defined as:

str3="$str1$str2" 

Note: Observe the above command; there shouldn't be any space before or after the assignment (=) operator. 'str' is employed to point strings.

This command will concatenate the values of str1 and str2 and store it during a third variable str3.

Example:

#Script to Concatenate Strings  
  
#Declaring the first String   
str1="We welcome you"  
  
#Declaring the Second String  
str2=" on Great Learning Academy."  
  
#Combining first and second string  
str3="$str1$str2"  
  
#Printing a new string by combining both   
echo $str3  

The Output of the above code.