1. Home
  2. Bash
  3. Examples
  4. Concatenate Two Strings

Concatenate Two Strings

Shell Script to Concatenate Two Strings

Brief: This example will help you to concatenate two or more strings variable in a bash script. This tutorial helps you with multiple shell script examples of concatenating strings in a shell script.

The first example is a general way to concatenate variables of string. You can simply write all the variable one after another:

Output:

Welcome TecAdmin!

Another Example

You can also use += operator to concatenate two strings and store results in the first string.

Output:

Welcome TecAdmin!

One More Example

Use another example with one string variable with user input and another fixed strings.

Script execution result:

Enter your name: Rahul
Welcome Rahul

Using {} with Variables

In some situation, you may face issue while concatenating one string with another string variable as shown in below example. Here you need to use curly braces around variable name.

Output

.sql
backup_03132018.sql

You can see that first output if incorrect and second output is correct using {} around variable name.

Tags , , ,