understanding Shell | Building basics of shell | Use cases of shell scripts

Subhradip SinhaSubhradip Sinha
3 min read

"hey readers, here is my second blog. I'll try to maintain this blog more interactive for better understanding about shells."

What exactly is a shell?🤔

In the realm of computing, the term "shell" often emerges, especially among developers and IT professionals. A shell is a command-line interpreter that provides a user interface for accessing the services of the operating system. It allows users to execute commands, run programs, and manage system resources. In essence, the shell acts as a bridge between the user and the operating system kernel.

"you can imagine a shell as a messanger that brings your command to the king as Operating System"

Types of shells

Now we have little concept about shells. In practical the working of shells are more complex. There are several types of shell available, each with its own set of features and functionalities

  • Bash (Bourne Again Shell)

The default shell on most Linux distributions.

  • Zsh (Z Shell)

Known for its user-friendly features and customization options.

  • Ksh (Korn Shell)

A shell with advanced scripting capabilities.

  • Fish (Friendly Interactive Shell)

A user-friendly shell with a focus on interactive use.

What's the role of a shell in OS?🤨

The shell's primary role is to interpret and execute user commands. When a user inputs a command, the shell parses it, interprets it, and then communicates with the operating system kernel to perform the requested action. The shell also provides features like command history, aliases, and job control to enhance user productivity. this way, users get more feasibility on controling over OS (without harming regulations and protocols of any OS)

Basic Shell Commands

Here are some basic shell commands. You can try those on your own

#LISTS THE FILES AND DIRECTORIES IN THE CURRENT DIRECTORY
ls
#CHANGES THE CURRENT DIRECTORY
cd
#PRINTS THE CURRENT WORKING DIRECTORY
pwd
#DISPLAYS A LINE OF TEXT OR VARIABLES
echo
#CONCATENATES AND DISPLAYS THE CONTENT OF FILES
cat

Shell and Shell Scripts

A shell is a command-line interpreter that allows users to interact with the operating system by entering commands. It serves as an interface between the user and the system kernel, translating user commands into actions performed by the operating system. whereas, shell script is a file containing a series of commands that the shell can execute. Shell scripts automate repetitive tasks, simplify complex operations, and can perform tasks such as file manipulation, program execution, and system monitoring. They are used to enhance productivity and ensure consistency in performing tasks.

How Shell Scripts Work?

When you execute a shell script, the shell reads the file line by line, interprets each command, and executes it. The #!/bin/bash at the beginning of the script specifies the interpreter to be used, which in this case is Bash.

Here's Your First Shell Script🤗

Creating a shell script involves writing commands in a text file and executing it. Here's a simple example:

  1. Create a new file named hello.sh:

     #!/bin/bash
     echo "Hello, World!"
    
  2. Save the file and give it execute permission:

     chmod +x hello.sh
    
  3. Run the script:

     ./hello.sh
    

Common Use Cases of Shell Scripts

Shell scripts are versatile and can be used for various tasks

  • Automating repetitive tasks: Simplifying and speeding up routine operations.

  • System administration: Managing system resources and configurations.

  • Batch processing: Handling multiple files or tasks in a single run.

  • Task scheduling: Automating tasks to run at specific times using cron jobs.

"Though building a shell requires lots of complex computing and logics in real world, an abstraction of shell can be created easily using any programming language (preferably low or mid level langauge). If this blog gets responses, i'll share an easy guide on building your very own shell using C"

"Don't forget to drop your valuable comments 🫶"

26
Subscribe to my newsletter

Read articles from Subhradip Sinha directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Subhradip Sinha
Subhradip Sinha