In this tutorial, learn different ways to append string variables

There are multiple ways to check if a string contains a substring

Simple variable append

Declare two string variables in the bash script printed to console using echo by appending(enclosed in a double quote with variable syntax)

string1="Hello, "
string2='Welcome to w3schools.'
echo "$string1 $string2 "

You can also append without a double quote

echo $string1 $string2


Hello, Welcome to w3schools.

Another example is to concatenate a string to the same variable and print it to the console

result="My site is"
result="${result} w3schools"
echo "${result}"


My site is w3schools

Append strings using shorthand arithmetic operator

shorthand arithmetic operator(+=) is used in numbers to add value to the variable and save the result to the variable.

This can also be used for strings to append a string to a variable.

For example,

  • a+=1 is equal to a=a+1 in case of numbers

  • str+=“test” will become str=str+“test” in the case of strings

Here is an example code

nums="One Two"
nums+=" three"
echo "$nums"


One Two three

Substring checks using the grep command

The grep command searches for a given string and piped to the main string to compare it

mainstring='Welcome to w3schools'

if echo "$mainstring" | grep -q "w3schools"; then
  echo "w3schools exists in the main string"