Bash or Shell script tutorial

What is shell?

shell is program which interprets user commands.command may be entered by user or by read from file or may be by file it (Shell) interpreter not compiled.

What is Shell script or Bash Script?

Shell script or Bash script is collection of multiple commands to automate a specific job, it will execute the command line by line.It translate the commands and sent to system.

 

Types of Linux shells?

Linux operating system are consists of kernel, shell, and GUI interface (Gnome, KDE, etc.).

Some popular Linux shell are:

Bourne shell − Default prompt is $.

C shell − Default prompt is %.

Categorizes of the Bourne Shell

sh shell – Bourne shell
ksh shell – Korn shell
bash – Bourne Again shell
POSIX shell (sh)

Categorizes of the C-type shells

csh – C shell
tcsh – TENEX/TOPS C shell

 

we can check shells on a Linux system from file /etc/shells

$ cat /etc/shells
/bin/bash
/bin/sh
/bin/tcsh
/bin/csh

we can check default shell by /etc/passwd file

$ less /etc/passwd
output:
kencorner:uehe7eiwfbi9eiuiue:ken kencorner:/home/kencorner:/bin/bash

for entering in another shell just enter name of new shell and hit enter.

 

Writing first shell scripts or bash scripts

step 1: open vi editor

vi firstScript.sh

Step 2: write these line

#!/bin/bash
echo “Hello pal”

then save and exit from vi editor (Esc :wq!)

Note : #!/bin/sh tell the system that command next to line be executed by the Bourne shell.It is known as shebang.
echo command will display the message to the monitor.

 

Executing the bash script

Before executing the command we need to change the mode of script

chmod 555 firstScript.sh (gives everyone read/execute permission)
or
chmod +rx firstScript.sh (gives everyone read/execute permission)
chmod u+rx firstScript.sh (gives only the script owner read/execute permission)

Now script is ready to run

./firstScript.sh

Output 

Hello pal

 

Comments in Bash script

# Single line comment


: '
This is a
multi line
comment


'

 

Bash script Example

In this below example we are interacting with user here we are using read keyword for taking input from user and storing his name in a variable ( which we will see in next lesson),

#! /bin/bash
echo "Enter Your First Name?"
read a
echo "welcome Mr./Mrs. $a, Please Enter your, Your Last Name"
read b
echo "Thanks Mr./Mrs. $a $b for telling us your name"
# Now Time to say good bye -- this is single line comment
: ' 
This is
a multi
line comment '

echo "Mr./Mrs. $b, it's time to say you good bye"

Now executing the script

chmod 555 <nameOfscript>

./nameofscript

Output

Enter Your First Name?
ken
welcome Mr./Mrs. ken, Please Enter your, Your Last Name
corner
Thanks Mr./Mrs. ken corner for telling us your name
Mr./Mrs. corner, it's time to say you good bye

 

More on echo in bash

echo \"{These,words,are,quoted}\" # " prefix and suffix

output

 "These" "words" "are" "quoted"

 

echo {a..z}

output

a b c d e f g h i j k l m n o p q r s t u v w x y z
echo {0..3}

output

0 1 2 3

Some More Topic Below :