Home » Bash Concatenate String

Bash Concatenate String

by Online Tutorials Library

Bash Concatenate String

In this topic, we have explained how to add or concatenate strings in Bash Shell Scripting.

In bash scripting, we can add or join two or more strings together, which is known as string concatenation. It is one of the common requirement for any programming language. A special character or built-in function is applied to perform string concatenation. However, Bash does not contain any built-in function to combine string data or variables. The easiest method to perform string concatenation in bash is to write variables side by side.

For example, assume that we have two strings (i.e., “welcome” & “to tutoraspire”), and we join both the strings together and a new string (“Welcome to Tutor Aspire”) is created. This concept is referred to as String Concatenation.

Command

The example command for concatenating the strings can be defined as:

Note: Observe the above command; there should not be any space before or after the assignment (=) operator. ‘str’ is used to indicate strings.

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

Following are some examples demonstrating the different ways of string concatenation:

Example 1: Write Variables Side by Side

This is the basic example of String Concatenation, and we do not need any extra operator or function in this method.

Bash Script

Output

We welcome you on tutoraspire.  

Example 2: Using Double Quotes

Another easy method is to use variables inside the string, which is defined with double-quotes. The string variable can be applied in any position of the string data.

Bash Script

Output

We welcome you on tutoraspire.  

Example 3: Using Append Operator with Loop

Most of the popular programming languages provide support for append operator (+=) which is the combination of the plus and equal sign. It will add new strings to the end of the string variable.

Bash Script

Output

Bash Concatenate String

Example 4: Using the Printf Function

In bash, printf is a function which is used to print and concatenate the strings.

Bash Script

Output

Welcome to Tutor Aspire.  

Example 5: Using Literal Strings

String concatenation can also be performed with a literal string by using curly braces{}. They should be used in such a way that the variable does not mix up with the literal string.

Bash Script

Output

Welcome to Tutor Aspire.  

Example 6: Using Underscore

Using underscore for concatenating the string in bash shell is one of the common tasks. It is mostly used for assigning a name to the files.

Bash Script

Output

Hello_World!  

Example 7: Using any Character

Bash Script

Output

Bash Concatenate String

Conclusion

String Concatenation is one of the required features in the programming languages to generate meaningful output. Almost all the possible ways of joining the strings in bash are explained in this topic.


Next TopicBash Functions

You may also like