Beginners Guide to Bash Shell Scripting

chaitanya4vedi

Chaitanya Chaturvedi

Posted on October 19, 2020

Beginners Guide to Bash Shell Scripting

BASH SHELL SCRIPTING

What is Bash?

Bash stands for ‘Bourne Again Shell’. Bash is a shell or command language interpreter for the GNU operating system. (GNU is basically a project to create a free operating system, wholly consisting of free softwares. It was initiated by Richard Stallman and was publicly announced in september 27, 1983. The Linux we use today is actually GNU-Linux). It is the default login shell for linux and most of the operating systems. You can also call it a command processor as it runs in a text window and takes commands from the user, executes it and performs actions accordingly. It can also read and execute commands from a file, called shell script. Like Unix shells, Bash also supports - wildcard matching, piping, here documents, command substitution, variables and control structures.

What is Bash Shell Scripting?

Bash (like many other shells) also has the ability to run an entire script of commands, known as a "Bash shell script" (or "Bash script" or "shell script" or just "script"). A script might contain just a very simple list of commands — or even just a single command — or it might contain functions, loops, conditional constructs, and all the other hallmarks of imperative programming. In effect, a Bash shell script is a computer program written in the Bash programming language.

Different Shells Available:

Sh shell, C Shell, Z Shell, Fish Shell, Korn Shell, etc.

Getting Started with Shell scripting

All you need is a plain text editor which usually comes with each and every operating system.
Example:
Nano, Vim or Vi, emacs, gedit, kate, text-editor, notepad, etc.

Step 1:

Open up a text-editor. Create a new file. Name it anything you want and save it with .sh extension.
Now the first line of a shell script should always start with a sha-bang or hash-bang => ‘#!’. It is a good practice to do this. This tells the system that it is a shell script. After that we give the location of the shell we write our script for, in this case it is Bash => ‘/bin/bash’

This is how it looks like:
# !/bin/bash

Note: ‘#’ without ‘!’ is used to write comments in shell scripts. Everything after ‘#’ in that line is ignored by the system.

Step 2:

Let's write a simple hello world shell script

#!/bin/bash
#Usage: Hello World Bash Shell Script
#Author: Chaitanya Chaturvedi
#-------------------------------------------------
#print it
echo "Hello!, World."

Now save the file. Open the terminal. Go to the locations where you script is located.
For the script to be executed by Bash, we first need to make it executable. We do that by
$chmod +x script-name.sh
And then execute it by
$./script-name.sh
Output:
Hello!, World.

Before we move forward lets get familiar with some shell commands and variables.

Echo is used to print text to the screen.
Example:
echo “Hello!, World”

Read is used to get input from the user
Example:
read n
Where n is a variable

We can declare a variable like
var_name=“Hello!, World”
And then can use it anywhere like
echo $var_name
Output:
Hello!, World

We can create new file using touch
Example:
touch “new_file”
This will create a file named new_file

To be continued.

You can follow me on twitter and instagram. I post informative content there as well.

Thanks for reading :)

💖 💪 🙅 🚩
chaitanya4vedi
Chaitanya Chaturvedi

Posted on October 19, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related