Table of contents
1.
Introduction
2.
Shell Scripting
2.1.
Characteristics of Shells
2.2.
Types of Shells
2.3.
Important Commands
2.4.
Starting with Shell Scripting
2.4.1.
Creating a Shell File
2.4.2.
Creating a Shell Script
3.
Frequently Asked Questions
3.1.
What are the two types of commands available for Bash Shell Scripting?
3.2.
Why do we use Shebang (#!)?
3.3.
Why do coders prefer Linux Operating System?
3.4.
Why is learning Shell so easy?
4.
Conclusion
Last Updated: Mar 27, 2024
Easy

INTRODUCTION TO SHELL SCRIPTING

Author Rupal Saluja
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

If you use Linux Operating System, it is implied that you must have used Shell Scripting. But do you know what makes it popular?

logo of linux

This is because Shell Script can save you a huge amount of time. You can completely automate hectic repetitive tasks. You can send your work to others to provide them with a script. These scripts perform System-level tasks, Data Backup, Programming, Automation, and Application related tasks. This must have generated an urge to learn more about Shell Scripting. 

logo of shell scripting

The article will discuss the foundation of Shell Scripting. This includes its various aspects and basic terminologies. So, without further ado, let’s get started. 

Shell Scripting

Before starting with Shell Scripting, ensure you have a Linux Operating System. Examples of Linux Operating Systems are Ubuntu, Devian, Fedora, Deepin, etc. Also, you are good to go if you have macOS or Windows 10. This is because nowadays, MacOS and Windows 10 also support shells.

So, first of all, what is a Shell?

A UNIX shell interprets user commands directly entered by the user or read from a file called the shell script or shell program. It is important to note that shell scripts are interpreted and not compiled. This means the shell processes the command a single line at a time. It differs from C or C++ programs translated entirely into a binary image through a compiler.

A Shell program may be very simple, containing a few shell commands, or quite complex, consisting of thousands of shell commands.

Characteristics of Shells

Some common characteristics of Shells are mentioned below. Note that it is not compulsory for Shells to checklist all the characteristics.

  • A Shell can be made using a text editor of your preference.
  • A Shell program is executed similarly to Shell commands. You only need to type the program's name along with Enter key.
  • A Shell program is a simple program consisting of primitive shell commands.
  • Shell programs are in free format. That means a program is correct if its shell commands follow the syntax. You can use blank lines, indentation, and whitespace freely.
  • Like any other program, shell programs allow input and output, logical decision-making, file creation and deletion, and system calls.
  • Shell programs have permission modes similar to any other file. You must have correct permissions set for program execution.

Types of Shells

Several types of Shells available are listed below.

  • Bourne Shell
  • Bash Shell
  • C Shell
  • TENEX Shell
  • Korn Shell
  • Z Shell
  • Fish Shell
     

In this article, we will focus mainly on the Bash Shell. This is because it is the standard shell that is intuitive and flexible. It is the default shell of many Linux distributions today. It is very well-compatible with other shell types and carries many improvements over previous shell types. These improvements include Command-line editing, Job Control, Infinite size command history, etc.

Important Commands

  • echo

It simply prints the line of text. It is similar to the ‘println’ keyword of Java.

  • file

It returns the type of script file.

  • type

It displays how the shell command’s arguments will be interpreted. That means whether the commands used will be read-only, write-only, read-write only, or read-write-executable.

  • whereis

It tells you the location of the source image, executable file, and manual pages of any shell command.

  • which

It is used to specify the file to be used for any shell script. You need to mention a particular file name here.

Starting with Shell Scripting

Pre-preparation

1. To start with Shell Scripting, open the terminal using the desktop icon or the shortcut key ‘Ctrl+Alt+T’.

open terminal screen

2. Now, check the shell types your Operating System supports. Use the command ‘cat /etc/shells’ for the same and press enter. You will see a list of shells your operating system supports.

shell types your operating system supports
list of available shell types

3. In this article, we are using Bash Shell. So, to know the location of this Bash Shell, use the ‘which bash’ command and press enter.

location of your bash

We have completed all the pre-preparation parts.

Creating a Shell File

We will clear the screen for a fresh start. To create a shell file, follow these steps.

1. Navigate to the Desktop folder or directory. Use the command in the image below.

2. Create a new shell script file in that directory, ‘hello.sh’. We will use the ‘touch’ mode for the same. Note that ‘.sh’ extension is not necessary for execution. We have used it here to differentiate a shell file from other files. It is considered a good practice to use this extension. Also, the ‘touch’ mode provides only read and write permissions. Use the command in the image below.

creation of new shell script

3. Please enter to create the ‘hello.sh’ file finally.

You will see a ‘hello.sh’ file on your desktop.

Creating a Shell Script

Now, we will create our first and very simple shell program. This bash shell program will print ‘Hello World’. Use the following steps for the proper execution. Note that you can use any editor of your choice. Double-click on the shell file to open the default editor. Here, we will use Visual Studio Code as our editor. We will use the ‘code .’ command to open the previously created shell file in Visual Studio Code.

opens shell file in VS Code

1. Open the editor and see the directory you imported using the above command. You will see an empty shell script.

imported directory appears

2. Use the command in the image below for proper execution.

command using our created shell script

3. Save the command and minimize the editor.

4. Execute the shell script in the terminal. It shows permission denied. It is because of the ‘touch’ mode we used.

execution of our created shell script

5. We will update the script’s permissions by adding ‘chmod’ mode. It will add ‘execute’ permission to the shell file.

updating file permissions

6. We will execute the script again. It prints ‘Hello World’.

execution of our created shell script again

You have now written your first successful shell script.

Frequently Asked Questions

What are the two types of commands available for Bash Shell Scripting?

The two types of commands available for Bash Shell Scripting are Operations on File and Directories. The ‘Operations on File’ include mv, cp, touch, rm. The Directories include pwd, Is, mkdir, cd, rmdir.

Why do we use Shebang (#!)?

Shebang (#!) is used at the start of the script or program. Developers use this to avoid repetition and tedious work. It is responsible for selecting the engine’s location used to run the script.

Why do coders prefer Linux Operating System?

Several developers and programmers prefer Linux Operating System. This is because Linux is more efficient and faster. It allows them to customize the operations according to their needs. This generates innovative and useful ideas.

Why is learning Shell so easy?

Shell permits users to communicate directly and very efficiently with their respective operating systems. It uses some natural language commands. Therefore, an individual can easily learn shell even with no programming background.

Conclusion

To cut it short, we understood what Shell Scripting is, its types, its characteristics, and some important commands. We also discussed how to start with Shell Scripting, that is, pre-preparation, shell file creation, and writing shell scripts.

We hope the above discussion helped you understand Shell Scripting more clearer and can be used for future reference whenever needed. To learn more about Ready API and its components, you can refer to blogs on Shell Scripting Interview QuestionsShell LoopsShell Scripting- Arithmetic Operations, and Shell Variables.

Visit our website to read more such blogs. Make sure you enroll in our courses, take mock tests, solve problems, and interview puzzles. Also, you can pay attention to interview stuff- interview experiences and an interview bundle for placement preparations. Do upvote our blog to help fellow ninjas grow.

Happy Coding!

Live masterclass