How to concatenate strings in Bash Shell?

In this tutorial, you will learn two different ways of concatenating strings in Bash:

  • Place two or more than two strings adjacent to each other.
  • Use the value of a variable inside another variable.

Concatenate strings by placing them next to each other

firstString="I am learning "
secondString=" Bash Shell Scripting."
thirdString=$firstString$secondString
echo $thirdString

Output of the above program

I am learning Bash Shell Scripting.

Note: This technique is used in IFS(Internal Field Separator) to specify more than one characters. Suppose you want to specify newline, semicolon, and colon as field separators then you will write IFS=$'\n';: or IFS='\n';:

Concatenate strings by using variable value inside another variable

firstString="Bash Shell Scripting"
secondString="$firstString is amazing."
echo $secondString

Output of the above program

Bash Shell Scripting is amazing.

Recommended Posts