Shell Scripting - 6
Shell Scripting - 6
Shell Scripting - 6
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.
A shell is an interface that interprets, processes, and executes these commands from the shell
script.
Page 1 of 6
In the above screenshot “ls” command with “-l” option is executed. It will list all the files in
the current working directory in a long listing format. Working with a command line shell is a
bit difficult for beginners because it’s hard to memorize so many commands. It is very
powerful; it allows users to store commands in a file and execute them together. This way any
repetitive task can be easily automated. These files are usually called batch files in Windows
and Shell Scripts in Linux/macOS systems.
Graphical Shells
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. A typical GUI in the Ubuntu system:
Page 2 of 6
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.
Shell Scripting
Shell scripting is a text file with a list of commands that instruct an operating system to perform
certain tasks. Shell scripting is a powerful tool commonly used across industries to automate
tasks, test solutions, and increase efficiency.
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., myscript.sh.
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.
Page 3 of 6
What is shell scripting used for?
Shell scripting is primarily used to automate repetitive system tasks, such as backing up files,
monitoring system resources, and managing user accounts. By turning a series of commands
into a script, system administrators can save time, increase accuracy, and simplify complex
tasks.
Who uses shell scripting?
Shell scripting is a valuable tool across several professions and fields due to its flexibility,
power, and widespread support of operating systems. The following are some roles that use
shell scripting:
System administrators: System administrators often use shell scripts for automating
administrative tasks like backups, system monitoring, user account creation and management,
and many more routine activities. This allows for efficiency, consistency, and accuracy.
Developers: Developers often use shell scripting to automate development tasks, like
automating file manipulation, deploying software to servers, running test suites, and more.
DevOps professionals: In the world of DevOps, shell scripting can benefit tasks such as
automation, configuration management, troubleshooting, and rapid iteration. Shell scripts can
run across operating platforms, which can be beneficial for those who work in several
environments.
Basic shell scripting terms
Before we delve further into shell scripting, knowing a few definitions may come in handy
when exploring this topic:
Terminal: A terminal is a program that establishes a connection with the server.
Shell: This program interprets shell scripting commands from the terminal and runs on
the server. This is the command-line user interface you choose and includes shells such
as the Bourne shell, Korn shell, Bourne-Again shell, and C shell.
Script: A script is a short program that performs a specific task.
Command-line shell: A command-line shell (also known as a command prompt)
allows you to instruct your computer through textual commands.
Shell script: A shell script is a script run through a command-line shell
When you create a shell script, you might use text editors like Nano or Vim and save the file
with a “.sh” extension. A shell interpreter interprets and executes these scripts; you can run
them from the command line whenever necessary.
5 types of shells
Different types of shells have different capabilities, so choose the type of shell that works best
for your applications. Your shell choice will affect how you can execute programs and which
system resources you can access. Some common types of shells you can choose from include:
Page 4 of 6
Type of
Description
shell
The Bourne shell, also known as ‘sh,’ was the original UNIX shell or command-
Bourne line interpreter developed by Stephen Bourne at AT&T Bell Labs. While known
shell for its high operation speed, this type of shell cannot reference previous commands
and has limited built-in functionality.
The C shell, ‘csh,’ is a shell developed by Bill Joy at the University of California
C shell Berkeley. This shell uses a similar syntax as the C programming language and
incorporates aliases into the available features.
Bash, short for “Bourne Again Shell,” is a shell created by Brian Fox as a
Bash
combination of sh, csh, and ksh capabilities. Bash is the default shell on Linux and
shell
Mac OSX. This type of shell can recall previous commands and edit them.
The Korn shell, or ‘ksh,’ is a shell developed by David Korn in the 1980s. It
Korn
combines features of both the Bourne and C shells, along with its own
shell
improvements, such as string and array manipulation.
The Z shell, or ‘zsh,’ is a modern extension of the sh shell and offers extensive
Z shell customization. Some notable features include plugins, function indexes, and
theming support.
Page 5 of 6
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.
Page 6 of 6