Bash script or Shell script

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

we can check default shell by /etc/passwd file

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

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),

Now executing the script

chmod 555 <nameOfscript>

./nameofscript

Output

More on echo in bash

output

 

output

output