What is Bash Shell?

Emin Ugar
Orion Innovation techClub
5 min readOct 15, 2021

Bash (Bourne-Again Shell) is a command interpreter that interprets commands typed into the terminal. These scripts are actually text files and doesn’t required to be compiled. You can also use the commands you use in the terminal in scripts. For example, (pwd, ls, cd etc.). It can validate files in scripts, get input from user, You can perform arithmetic and logical operations. You can create and use functions. You can automate your processes using scripts. This way you don’t repeat yourself. You can also use bash script and version control systems (GitHub, git, Jenkins etc.)
You can automate your processes in the CI pipeline. (Build, unit test, coverage report, file export etc.) Another operation you can do using bash script is uploading and downloading to the network environment.
If you know any programming language (java, c#, c etc.) you can notice the similarity of the scripts.

How to write bash scripts?

In this article, we will make basic script examples.

  • Variable definition and printing to the screen.
  • Receiving data from user
  • if else
  • Arithmetic and logical operations.
  • Case
  • For loop
  • While loop
  • Create and use function

I will use the Visual Studio Code editor to write the script file. When saving the file, we save it with the .sh extension. To run the .sh file, right-click on the folder where the file is located and open bash terminal. We run the file with the ./fileName.sh command in the terminal.

We add the command “#!/bin/bash” to the first line of our file to be able to use the bash script. Furthermore, we need to make sure that this command is written on the first line. This string of statements is called the shebang, and the line starting with this string is called the shebang line. Specifies the path to the interpreter to use for the script to run.

Now let’s start making our examples.

1- Variable definition and printing to the screen.

Example 1
  • str : It is my variable name (In double quotes because it’s a string)
  • echo : It is used to print output to the screen.
  • $str: Variable pointer
Output of example 1

2- Variable definition and printing to the screen.

Example 2
  • echo -e “……:\c” : So that the cursor stays on the same line.
  • read: Assigns read data to variable
Output of example 2

3- if else

Example 3
  • if [ “$x” -gt “$y” ] : Conditions are written in square brackets. A space is left in front of the square brackets.
  • then : Actions are written here.
  • elif : Different spelling of “else if”
  • fi : End line of if
Output of example 3

4- Arithmetic and logical operations.

  • Arithmetic operations
Example 4
  • Double parentheses are used for arithmetic operations.
Output of example 4
  • Logical operations
  • -eq (equal): if[ “$a” -eq “$b” ]
  • -ne (not equal): if[ “$a” -ne “$b” ]
  • -gt (greater than): if[ “$a” -gt “$b” ]
  • -ge (greater than or equal): if[ “$a” -ge “$b” ]
  • -lt (less than): if[ “$a” -lt “$b” ]
  • -le (less than or equal): if[ “$a” -le “$b” ]

OR

  • == (equal): if(( “$a” == “$b” ))
  • != (not equal): if(( “$a” != “$b” ))
  • > (greater than): if(( “$a” > “$b” ))
  • >= (greater than or equal): if(( “$a” >= “$b” ))
  • < (less than): if(( “$a” < “$b” ))
  • <= (less than or equal): if(( “$a” <= “$b” ))

5- Case

Example 5
  • case … in : Check of value.
  • 1 ),2 ),3 ) : Actions where the value overlaps.
  • *) : Actions for other values
  • esac : End line of case
Output of exaple 5

6- For loop

Example 6
  • Method 1 : Classic for loop. Conditions are written in double parenthesis. Actions are written after the do line. done line is end line of loop.
  • Method 2 : {a..b..c} a is initial value of the loop, b is end value of the loop and c is amount of increase of the loop.
  • Method 3 : (in x y z) x,y,z is the values ​​of loop.
Output of example 6

7- While loop

Example 7

  • Conditions are written in double parenthesis. Actions are written after the do line. done line is end line of loop.
Output of example 7

8- Create and use function

Example 8
  • “sayHello” is my function name. The functions of sayHello are written between the parentheses. When we want to call it, we just need to write the name of the function, like line 11.
Output of example 8

--

--