UNIT 3: Introduction to Shell
Scripts
What is Kernel?
The kernel is a computer program that is the core of a computer’s operating system, with complete
control over everything in the system. It manages the following resources of the Linux system –
● File management
● Process management
● I/O management
● Memory management
● Device management etc.
What is Shell?
A shell is a special user program that provides an interface for the user to use operating system
services. Shell accepts human-readable commands from users and converts them into something
which the kernel can understand. It is a command language interpreter that executes commands read
from input devices such as keyboards or from files. The shell gets started when the user logs in or
starts the terminal.
Shell is broadly classified into two categories –
● Command Line Shell - Shell can be accessed by users using a command line interface. A special program
called Terminal in Linux/macOS, or Command Prompt in Windows OS is provided to type in the
human-readable commands such as “cat”, “ls” etc. and then it is being executed.
● Graphical shell - Graphical shells provide means for manipulating programs based on the
graphical user interface (GUI), by allowing for operations such as opening, closing, moving, and
resizing windows, as well as switching focus between windows. Window OS or Ubuntu OS can
be considered as a good example which provides GUI to the user for interacting with the
program. Users do not need to type in commands for every action
There are several shells are available for Linux systems like –
● BASH (Bourne Again SHell) – It is the most widely used shell in Linux
systems. It is used as default login shell in Linux systems and in macOS. It
can also be installed on Windows OS.
● CSH (C SHell) – The C shell’s syntax and its usage are very similar to the C
programming language.
● KSH (Korn SHell) – The Korn Shell was also the base for the POSIX Shell
standard specifications etc.
Each shell does the same job but understands different commands and provides
different built-in functions.
What is a terminal?
A program which is responsible for providing an interface to a user so that he/she can access the
shell. It basically allows users to enter commands and see the output of those commands in a
text-based interface. Large scripts that are written to automate and perform complex tasks are
executed in the terminal.
To access the terminal, simply search in search box “terminal” and double-click it.
Shell Scripting
Usually, shells are interactive, which means they accept commands as input from users and execute
them. However, sometimes we want to execute a bunch of commands routinely, so we have to type in all
commands each time in the terminal.
As a shell can also take commands as input from file, we can write these commands in a file and can
execute them in shell to avoid this repetitive work. These files are called Shell Scripts or Shell
Programs. Shell scripts are similar to the batch file in MS-DOS. Each shell script is saved with `.sh` file
extension e.g., [Link].
A shell script has syntax just like any other programming language. If you have any prior experience with
any programming language like Python, C/C++ etc. It would be very easy to get started with it.
A shell script comprises the following elements –
● Shell Keywords – if, else, break etc.
● Shell commands – cd, ls, echo, pwd, touch etc.
● Functions
● Control flow – if..then..else, case and shell loops etc.
Why do we need shell scripts?
There are many reasons to write shell scripts:
● To avoid repetitive work and automation
● System admins use shell scripting for routine backups.
● System monitoring
● Adding new functionality to the shell etc.
Some Advantages of shell scripts
● The command and syntax are exactly the same as those directly entered in the command
line, so programmers do not need to switch to entirely different syntax
● Writing shell scripts are much quicker
● Quick start
● Interactive debugging etc.
Some Disadvantages of shell scripts
● Prone to costly errors, a single mistake can change the command which might be harmful.
● Slow execution speed
● Design flaws within the language syntax or implementation
● Not well suited for large and complex task
● Provide minimal data structure unlike other scripting languages. etc.
SHELL is a program which provides the interface between the user and an operating system. When the
user logs in OS starts a shell for user.
Kernel controls all essential computer operations, and provides the restriction to hardware access,
coordinates all executing utilities, and manages Resources between process. Using kernel only user can
access utilities provided by operating system.
What is the Bourne shell?
The Bourne shell is the original Unix shell -- command execution program, often called a
command interpreter -- that was developed in 1979 at what at the time was Bell Labs.
Named for its developer, English computer scientist Stephen Bourne, the Bourne shell is
also known by its program name, sh. The Bourne shell family also includes the Korn shell,
bash (Bourne Again Shell) and the Z shell (zsh).
The Bourne shell is used for scripting. It provides command-based programming to interpret
and execute user commands. As a user types a command, the shell interprets it so the
operating system can take action, such as automating a task. The Bourne shell was the
default shell for Unix version 7. The shell prompt used in command prompts is the $ symbol.
Bash is the free replacement version of the Bourne shell that's distributed with Linux
systems. Bash is similar to the original Bourne shell but has added features, such as
command line editing. Its name is sometimes spelled as "Bourne Again SHell" -- the
capitalized "Hell" referring to the difficulty some users have with it.
The Bourne shell (sh) is the original UNIX shell developed by Stephen Bourne at Bell Labs in the
1970s. It is a command-line interpreter that provides a scripting language for automating tasks.
● Default shell on many UNIX systems before being superseded by Bash (Bourne Again Shell).
● It is POSIX-compliant, which means scripts written in it are highly portable.
Introduction to C Shell (csh)
The C Shell (abbreviated as csh) is a UNIX command-line interpreter and scripting language
that provides an interface between the user and the operating system kernel. It allows users to
execute commands, run programs, and automate tasks using shell scripts.
The shell is called "C Shell" because its syntax and control structures are similar to the C
programming language. This resemblance made it popular among programmers familiar with C
during its time of origin.
Developed in the late 1970s by Bill Joy at the University of California, Berkeley.
Introduced as part of BSD UNIX (Berkeley Software Distribution).
Intended as an alternative to the Bourne Shell (sh), providing enhanced interactive features.
The C shell is an interactive command interpreter and a command programming
language. It uses syntax that is similar to the C programming language.
The csh command starts the C shell.
VARIABLE - Alternate Name for memory locations
Variable Names
1. Variable name should start with either alphabet or underscore.
2. Variable name should not contain any special characters other than
underscore.
SCALAR VARIABLES - Normal variables are scalar variables
Defining Variable :
Variable_name = variable_value/expression
Accessing Variables :
Prefix variable with $
Ex : a = 10
$a
Read-Only Variable :
readonly variable_name
Shell Scripting
Usually, shells are interactive, which means they accept commands as input from users and
execute them. However, sometimes we want to execute a bunch of commands routinely, so we
have to type in all commands each time in the terminal.
As a shell can also take commands as input from file, we can write these commands in a file and
can execute them in shell to avoid this repetitive work. These files are called Shell Scripts or Shell
Programs. Shell scripts are similar to the batch file in MS-DOS. Each shell script is saved with
`.sh` file extension e.g., [Link].
A shell script has syntax just like any other programming language. If you have any prior
experience with any programming language like Python, C/C++ etc. It would be very easy to get
started with it.
A shell script comprises the following elements –
● Shell Keywords – if, else, break etc.
● Shell commands – cd, ls, echo, pwd, touch etc.
● Functions
● Control flow – if..then..else, case and shell loops etc.
Why do we need shell scripts?
There are many reasons to write shell scripts:
● To avoid repetitive work and automation
● System admins use shell scripting for routine backups.
● System monitoring
● Adding new functionality to the shell etc.
Some Advantages of shell scripts
● The command and syntax are exactly the same as those directly entered in the command
line, so programmers do not need to switch to entirely different syntax
● Writing shell scripts are much quicker
● Quick start
● Interactive debugging etc.
Some Disadvantages of shell scripts
● Prone to costly errors, a single mistake can change the command which might be harmful.
● Slow execution speed
● Design flaws within the language syntax or implementation
● Not well suited for large and complex task
● Provide minimal data structure unlike other scripting languages. etc.
Metacharacters in Linux
🔹 Definition:
Metacharacters are special symbols used by the shell to interpret commands, match files, or perform
operations. These characters have special meanings and are not treated as ordinary characters by the
shell.
Environment Inheritance
● When a shell spawns a child process, it passes a copy of the environment variables.
● Changes in child processes do not affect the parent shell.
Environment in Linux
🔹 Definition:
The environment in Linux refers to a collection of variables and settings that define the behavior of the
shell and processes. It includes variables that affect user sessions, command execution, and system
behavior.