Getting started with Bash Shell Scripting

Shell is a program which interprets user commands through CLI like Terminal. Shell scripting is writing a series of command for the shell to execute.

Bash is a “Unix shell”: a command line interface for interacting with the operating system. Bash shell script is a computer program written in the Bash programming language.

To know which types of shells are supported by your OS type command:

$ cat /etc/shells


To know where bash is located, type command:

$ which bash
  /bin/bash

1 . Creating bash script

Let’s create a file and open it with text editor such as vim or nano.

$ touch hello.sh
$ nano hello.sh

In the first line we write the location of the bash, it is called a shebang or a “bang” line and in the second line we write the script. Save it and then execute the script.

#!/bin/bash

echo "hello world"
$ ./hello.sh

It shows permission is denied, because when we create file using touch it gives only read and write permissions to the file, hence it is not executable . You can check permissions of file using command :

$ ls -la


Now make the file executable using Chmod command and then again execute the file hello.sh

$ chmod +x hello.sh
$ ./hello.sh


2 . Comments and Variables

A word or line beginning with # causes that word and all remaining characters on that line to be ignored. These lines aren’t statements for the bash to execute, the bash totally ignores them and these lines are called comments.

Comments are basically explanatory text about script. It makes source code easier to understand.

To understand, open the file hello.sh using text editor and write the comments. Save it and then execute.

$ nano hello.sh 
#!/bin/bash

#this is a comment
echo "hello world" #this is the script line1

variable is a character string to which we assign a value. The value assigned could be a number, text, filename, device, or any other type of data. A variable is nothing more than a pointer to the actual data. The shell enables you to create, assign, and delete variables.

There are two types of variables :

  1. System Variables : Created and maintained by Linux bash shell itself. This type of variable is defined in CAPITAL LETTERS.
  2. User defined Variables : Shells also allow you to create your own variables for use within scripts, user variables are created using lower-case characters. 

To better understand two variables let’s write some variables in our file and then execute.

#!/bin/bash

#this is a comment
echo "hello world" #this is the script line1

echo $BASH
echo $BASH_VERSION
echo $HOME
echo $PWD


Variables which we used were System variables :

  • $BASH : defines type of shell you are using
  • $BASH_VERSION : it gives the version of the bash in use.
  • $HOME : it shows your home directory.
  • $PWD : it shows your present working directory.

Now we will create some User defined variables:

#!/bin/bash

#this is a comment
echo "hello world" #this is the script line1

echo $BASH
echo $BASH_VERSION
echo $HOME
echo $PWD

name=Nupur
echo $name

So here we created a Variable called name, which has a value “Nupur”.



You can also write these variables forming sentences :

#!/bin/bash

#this is a comment
echo "hello world" #this is the script line1

echo $BASH
echo $BASH_VERSION
echo Our home directory is $HOME
echo $PWD

name=Nupur
echo I am $name


Note : A variable name should not start with number.

3 . Read User Input

Let’s create a new file using text editor , save your file, change permissions and then execute.

$ nano names.sh
#1/bin/bash

echo "Enter name: "
read name
echo "Entered name: $name"

Read is a command found on Unix and Unix-like operating systems such as Linux. It reads a line of input from standard input or a file passed as an argument to its -u flag, and assigns it to a variable.

Here User will enter “name” , the input will be stored in the variable name. So read command helps to store the input into a variable.

$ chmod +x names.sh
$ ./names.sh


For using Multiple variables :

#!/bin/bash

echo "Enter names: "
read name1 name2 name3
echo "Entered names: $name1 , $name2 , $name3"

For separating input read variables uses spaces ( ) and for printing, separate variables using coma ( , )



To get input on the same line :

Create a file and open it with text editor .

$ nano user.sh
#!/bin/bash

read -p "Username: " user_var
echo "Username: $user_var"

The –p option makes it read as a prompt, meaning it doesn’t add a trailing newline before trying to read input.

Input of Username is stored in Variable user_var.

$ chmod +x user.sh
$ ./user.sh


Suppose we also need to add password but we do not want our password to be visible, while typing. For this we use -sp flag .

The -s option do not echo input coming from a terminal.

#!/bin/bash

read -p "Username: " user_var
echo "Username: $user_var"

read -sp "Password: " pass_var
echo 

echo "Password: $pass_var"

We will use -sp so that our password is not visible when we are typing and then we will use echo command to check our input as it is stored in the pass_var.



To store inputs in an array :

Let’s remove everything in the script and write some new lines:

#!/bin/bash

echo "Enter names: "
read -a names
echo "Names: ${names[0]}, ${names[2]}"

The – a option assign the words read to sequential indices of the array variable ARRAY, starting at zero.

The input in stored in variable names, we use curly braces { } to print variables. Index numbers [ ] are used to print particular variables.



Now suppose if we do not enter any flag with the read option , how can we store our input. For this purpose we use REPLY Variable.

REPLY is a default build-in variable which stores the input by user.

#!/bin/bash

echo "Enter names: "
read
echo "Names: $REPLY"


To learn more about bash scripting move to next blog.