BASH SCRIPTING

Grace Omokhowa Audu
4 min readJul 17, 2024

--

Bash is an acronym for “Bourne Again Shell.” It is the default shell for the Linux operating system. Bash scripting is used for automating tasks in Linux.

How to write a bash script

Steps in writing a bash script:

  1. Create a file with the .sh extension.
  2. Write down a bash script within it.
  3. Provide execution permission for it.

There are different editing commands you can use to edit in Bash scripts, like Nano, Gedit, and Vim, but I am going to be working with Vim.

Why do we use Bash scripting?

  • For security
  • For processing data
  • For system administration
  • For automation of tasks.

Basic Commands in Bash

Echo: It displays text.

Cat: It displays the content in a file.

Vim: It is used to create and edit a file.

To write something in Vim, type “I,” which means insert mode. From this mode, start writing text. Press the “escape” button to change the insert mode to command mode. Press colon (:) w to save the file and colon (:) q to quit.

To view what you have saved, type the “./” and the filename.

NOTE: When you type the name of the shell, you won’t be able to view it because you have no permission to execute it. To set the script’s execution privileges, type chmod +x along with the shell name. e.g., chmod +x hellothere.sh

And type./hellothere.sh to see the command you have typed in this shell.

These are the structures of a scripting language:

  • Input and Output
  • Arguments, Variables, and Arrays
  • Conditional execution
  • Arithmetic
  • Loops
  • Comparison operators
  • Functions

INPUT AND OUTPUT

In the image below, I created a bash script called bash.sh. I used the Vim editor to create it. Take a look at the command below:

  1. I created a file called firstbash.sh with vim and clicked enter. The enter button took me to the second image.

2. This is the image the enter button brought me to. I typed “I” for insert mode and typed “shebang” (#!/bin/bash). I typed echo before writing my bash script. I clicked the “escape key” before I typed :wq (to save and quit).

3. The wq above brought me back to this terminal, where I tried to view the file. Image A: I tried to view the file, but permission was denied.

4. Image B: I gave permission to execute the file with the “chmod” command. This was done so I could use the “./” to view the file.

VARIABLES IN BASH

This is assigning a command to a variable. It is used for storing and manipulating data.

  1. I created a file named “Variable.sh.”.

For example, MY_FIRST_NAME=Grace

MY_LAST_NAME=Audu

Echo Hello $MY_FIRST_NAME

Echo $MY_LAST_NAME

Save and quit the terminal with:wq

2. I wrote the variable script.

3. The output of the script.

CONDITIONAL EXECUTION

Conditional execution allows us to control the flow of our script by reaching different conditions. Components used in conditional execution:

#!/bin/bash — Shebang: It tells the operating system the interpreter to use.

if-else-fi: conditional execution.

echo: prints the specific output.

$#, $0, or $1 are special variables.

domain: variables.

  1. I created a function.sh file.

2. I wrote the condition for the script.

3. Condition output.

CASE STATEMENTS

It matches an expression with a pattern in clauses.

  1. I created a file named login.sh.

2. I wrote the case statement in the script.

3. Case statement output.

ARRAYS

It is used for storing and retrieving elements in a list.

Here is an example of how to write the command.

To print just the first element. We will use this command: echo ${MY_FIRST_LIST[0]}

Note: The zero in the square bracket represents the first element. If we were to print the second element, we would have typed 1 in the square bracket. In Bash, the first item is an array always represented with index zero and counts upwards.

To conclude, by learning the Bash script you will be able to automate tasks and this will increase your productivity.

--

--