Concatenate string variables

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:


Welcome TecAdmin!

Another Example

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


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.



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

Check if two strings are equal?

Brief: This example will help you to understand to check if two strings are equal in a bash script. This shell script accepts two string in variables and checks if they are identical.

Bash Strings Equal Check:

  • Use == operator with bash if statement to check if two strings are equal.
  • You can also use != to check if two string are not equal.
  • You must use single space before and after the == and != operators.


In this script two variables are initialized with predefined strings. Now check if both strings are equal or not with == operator.


Strings are equal

Another Example:

Use below sample shell script to take input from the user and check if given strings are equal or not.

Script execution results:

First Run:
StEnter first string: welcome bash
Enter second string: welcome bash
Strings are equal

Second Run:
Enter first string: Welcome bash
Enter second string: hello bash
Strings are not equal

Add two numbers?

Shell Script to Add Two Integers

Brief: This example will help you to understand to add two numbers in the bash script. This script takes the input of two numbers from the user and prints the sum of both numbers.

This sample script initializes two variables with numeric values. Then add two number variables and store in another variable.

The below shell script uses read command to prompt the user for two numbers. Then calculate the sum of given numbers and store to another variable.


Enter first number: 12
Enter second number: 14
Sum is: 26

MongoDB – mongo Shell

What is Mongo Shell?

MongoDB provides an interactive mongo shell based on JavaScript. This provides a command line interface between users and MongoDB databases. You can find the best tutorials here for the installation of MongoDB server on Linux operating systems.

What is mongo Shell?

Mongo shell is a command line interface between user and database. You can connect to your MongoDB server using this shell and manage your databases and collections. You can also perform the administrative tasks on MongoDB.

Connect to MongoDB Shell

Type mongo on your system terminal. It will automatically connect to your local MongoDB server running on port 27017.


MongoDB shell version v3.4.4
connecting to: mongodb://
MongoDB server version: 3.4.4

You need to specify the hostname to connect remote database. Also, specify port if MongoDB is running on different port.

> mongo --host --port 27017

MongoDB shell version: 2.6.4
connecting to:

Exit the mongo Shell

Use quit() function or press CTRL+C to quit from mongo shell.

> quit()

Working with MongoDB Shell

The ongoing tutorial is based on mongo shell. Let’s do some basic tasks with mongo shell. Open mongo shell as shown in above commands and run following commands.

Arithmetic operations:

> 22 + 10          ## Output: 22
> 22 * 10          ## Output: 220

Using variables:

> i=22              ## Output: 22
> j=10              ## Output: 220
> i * j             ## Output: 220

Bash – For Loop

For Loop in Bash

As any other programming language, shell scripts also support for loops to do the repetitive tasks. Read this tutorial to understand uses of for loop with examples.


  // scope of for loop

for loop executes for each parameter once defined. The VARIABLE is initialized with the parameter’s value which can be accessed in inside the for loop scope. These parameters can be any number, string etc.

#1. Bash – For Loop Example

Check below basic for loop which iterates 5 times.

You can also define a range with for loop in the bash script with numeric values.

The arguments can be also a string like:

#2. Bash – For Loop in C Style

You can also write for loop in bash script similar to for loop in c programming. For example to print 1 to 10 numbers.

#3. Bash – For Loop with Files

You can access filenames one by one in for loop under the specified directory. For example, read all files from the current directory.

The above loop will iterate the number of times as the number of files available. It will select one by file in each iteration.

Bash – Quotes

Quotes in Bash

This is a standard practice to quote the string in any programming language. Quotes are used to deal with the texts, filenames with a space character. Read this tutorial to understand the differences between single quote and double quotes.

Quote with String

While working with simple texts and string, there are no different in using a single quote or double quote.

The above script will run without any error and print the messages and create both directories.

Quote with Variables

Just remember that the shell variable expansion will work with double quotes only. If you define any variable in single quote will not work.


NAME="Welcome TecAdmin"

echo "$NAME"
echo '$NAME'

Now execute this script. The first echo will print the value of variable ie “Welcome TecAdmin”, But the second variable will print only $NAME due to enclosed in single quote.

Bash – Variables

Variables in Bash

The bash variables are same as other programming languages. You don’t need to specify the type of variable in bash scripting. Read below example.


NAME="TecAdmin Tutorials"
echo $NAME

launchdate="Feb 08, 2013"
echo $launchdate

Global vs Local Variables

Global variables are accessible anywhere in the script. Where Local variables are accessible in scope only. For example, a variable that is used inside a function only.

System Variables

System variables are responsible to define the aspects of the shell. These variables are maintained by bash itself. But we can modify these variables to change shell aspects.

Type env on bash shell to print all the available variables with there value.

$ env

Generally, these variables are defined in capital letters. For example PATH, SHELL, HOME, LANG, PWD and many more.

Shell scripting

Bash Tutorial

Shell Scripting

The Bash (Born Again) is a Unix shell and command line language written by the Brian Fox. The Born Again shell is used by most of the Unix/Linux operating systems as there default shell.

Bash Scripting Tutorial

This Shell Script tutorial is written for the peoples want to learn the basics of shell script programming (Shell scripting). In this series of tutorials, you will learn about bash scripting, which is very useful for automating the daily tasks, larger tasks easier. It helps to automation of tasks like backups, disk cleanup etc.