Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Aptitude
  • Engineering Mathematics
  • Discrete Mathematics
  • Operating System
  • DBMS
  • Computer Networks
  • Digital Logic and Design
  • C Programming
  • Data Structures
  • Algorithms
  • Theory of Computation
  • Compiler Design
  • Computer Org and Architecture
Open In App
Next Article:
How to Create a Shell Script in linux
Next article icon

Introduction to Linux Shell and Shell Scripting

Last Updated : 16 Apr, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

If we are using any major operating system, we are indirectly interacting with the shell. While running Ubuntu, Linux Mint, or any other Linux distribution, we are interacting with the shell by using the terminal. In this article we will discuss Linux shells and shell scripting so before understanding shell scripting we have to get familiar with the following terminologies:

  • Kernel
  • Shell
  • Terminal

Table of Content

  • What is Kernel?
  • What is Shell?
  • Command Line Shell
  • Graphical Shells
  • What is a terminal?
  • Shell Scripting

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.

It is often mistaken that Linus Torvalds has developed Linux OS, but actually, he is only responsible for the development of the Linux kernel.

Complete Linux system = Kernel + GNU system utilities and libraries + other management scripts + installation scripts.

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.

Linux Shell

Linux Shell

Shell is broadly classified into two categories –

  • Command Line Shell
  • Graphical shell

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. The result is then displayed on the terminal to the user. A terminal in Ubuntu 16.4 system looks like this –

linux command line

linux command line

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 –

GUI Shell

GUI Shell

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.

open terminal

open terminal

Here you can see how the terminal looks of Red Hat Linux.
 

terminal

terminal

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

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.

Simple demo of shell scripting using Bash Shell

If you work on a terminal, something you traverse deep down in directories. Then for coming few directories up in path we have to execute a command like this as shown below to get to the “python” directory:

get to the “python” directory:

get to the “python” directory:

It is quite frustrating, so why not we can have a utility where we just have to type the name of directory and we can directly jump to that without executing the “cd ../” command again and again. Save the script as “jump.sh”

bash
# !/bin/bash  # A simple bash script to move up to desired directory level directly  function jump() {     # original value of Internal Field Separator     OLDIFS=$IFS      # setting field separator to "/"     IFS=/      # converting working path into array of directories in path     # eg. /my/path/is/like/this     # into [, my, path, is, like, this]     path_arr=($PWD)      # setting IFS to original value     IFS=$OLDIFS      local pos=-1      # ${path_arr[@]} gives all the values in path_arr     for dir in "${path_arr[@]}"     do         # find the number of directories to move up to         # reach at target directory         pos=$[$pos+1]         if [ "$1" = "$dir" ];then              # length of the path_arr             dir_in_path=${#path_arr[@]}              #current working directory             cwd=$PWD             limit=$[$dir_in_path-$pos-1]             for ((i=0; i<limit; i++))             do                 cwd=$cwd/..             done             cd $cwd             break         fi     done } 

For now, we cannot execute our shell script because it does not have permissions. We have to make it executable by typing following command –

$ chmod +x path/to/our/file/jump.sh

Now to make this available on every terminal session, we have to put this in “.bashrc” file.

“.bashrc” is a shell script that Bash shell runs whenever it is started interactively. The purpose of a .bashrc file is to provide a place where you can set up variables, functions, and aliases, define our prompt, and define other settings that we want to use whenever we open a new terminal window.

Now open the terminal and type the following command:

$ echo “source ~/path/to/our/file/jump.sh”>> ~/.bashrc

Now open your terminal and try out new “jump” functionality by typing following command-

$ jump dir_name

just like the below screenshot:

jump dir_name

jump dir_name

Conclusion

In this article, we learned about the essential parts of Linux systems: the kernel, which controls everything, the shell, which lets us interact with the operating system, and the terminal, our interface to give commands. We explored command line and graphical shells, like BASH, and understood that the terminal is where we type in our commands. The article introduced shell scripting, a way to automate tasks using simple scripts, and discussed the advantages and disadvantages of using them. A practical example demonstrated creating a script for quick directory navigation. Finally, we saw how to make the script work and become accessible in every terminal session. This article is a beginner-friendly guide to understanding and using Linux shells and shell scripting.linux is good language it is written in linux language



Next Article
How to Create a Shell Script in linux
author
kartik
Improve
Article Tags :
  • Linux-Unix
  • Operating Systems

Similar Reads

  • Introduction to Linux Shell and Shell Scripting
    If we are using any major operating system, we are indirectly interacting with the shell. While running Ubuntu, Linux Mint, or any other Linux distribution, we are interacting with the shell by using the terminal. In this article we will discuss Linux shells and shell scripting so before understandi
    7 min read
  • Introduction to Shell Scripting

    • How to Create a Shell Script in linux
      Shell is an interface of the operating system. It accepts commands from users and interprets them to the operating system. If you want to run a bunch of commands together, you can do so by creating a shell script. Shell scripts are very useful if you need to do a task routinely, like taking a backup
      7 min read

    • Different Shells in Linux
      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 b
      5 min read

    Basic Shell Commands in Linux

    • Basic Shell Commands in Linux: Complete List
      Anyone using Linux should become an expert in the essential shell commands, as they form the backbone of working with the Linux terminal. These commands enable you to navigate the system, manage files, handle processes, and configure settings effectively. The Linux shell serves as an interface for u
      5 min read

    • Linux Directory Structure
      Prerequisite: Linux File Hierarchy Structure In Linux/Unix operating system everything is a file even directories are files, files are files, and devices like mouse, keyboard, printer, etc are also files. Here we are going to see the Directory Structure in Linux. Types of files in the Linux system.
      5 min read

    • Input Output Redirection in Linux
      In Linux, whenever an individual runs a command, it can take input, give output, or do both. Redirection helps us redirect these input and output functionalities to the files or folders we want, and we can use special commands or characters to do so. For example, if we run the "date" command, it giv
      4 min read

    Variables and Data Types

    • Shell Scripting - Shell Variables
      A shell variable is a character string in a shell that stores some value. It could be an integer, filename, string, or some shell command itself. Basically, it is a pointer to the actual data stored in memory. We have a few rules that have to be followed while writing variables in the script (which
      6 min read

    • Shell Scripting - Rules for Naming Variable Name
      Variables are quite important in any script or program, so we need to understand what is the convention to name these variables. There are certain rules and standards to keep in mind while giving names to the variables in Shell scripting. In this article, we will discuss and list down all the rules
      4 min read

    • String Manipulation in Shell Scripting
      String Manipulation is defined as performing several operations on a string resulting change in its contents. In Shell Scripting, this can be done in two ways: pure bash string manipulation, and string manipulation via external commands. Basics of pure bash string manipulation: 1. Assigning content
      4 min read

    • Array Basics in Shell Scripting | Set 1
      Consider a situation if we want to store 1000 numbers and perform operations on them. If we use a simple variable concept then we have to create 1000 variables and perform operations on them. But it is difficult to handle a large number of variables. So, it is good to store the same type of values i
      6 min read

    Control Structures

    • Conditional Statements | Shell Script
      Conditional Statements: There are total 5 conditional statements which can be used in bash programming if statement if-else statement if..elif..else..fi statement (Else If ladder) if..then..else..if..then..fi..fi..(Nested if) switch statement Their description with syntax is as follows: if statement
      3 min read

    • Looping Statements | Shell Script
      Looping Statements in Shell Scripting: There are total 3 looping statements that can be used in bash programming  Table of Content `while` statement in Shell Script in Linux`for` statement in Shell Script in Linux`until` statement in Shell Script in LinuxExamples of Looping StatementsTo alter the fl
      10 min read

    • Break and Continue Keywords in Linux with Examples
      Both “break” and “continue” are used to transfer control of the program to another part of the program. It is used within loops to alter the flow of the loop and terminate the loop or skip the current iteration. break The break statement is used to terminate the loop and can be used within a while,
      3 min read

    Functions and Script Organization

    • Shell Scripting - Functions and it's types
      Shell scripting is a powerful tool used to automate tasks in Unix-like operating systems. A shell serves as a command-line interpreter, and shell scripts often perform file manipulation, program execution, and text output. Here, we'll look into functions in shell scripting, exploring their structure
      5 min read

    • How To Pass and Parse Linux Bash Script Arguments and Parameters
      Parsing and Passing of Arguments into bash scripts/ shell scripts is quite similar to the way in which we pass arguments to the functions inside Bash scripts. We'll see the actual process of passing on the arguments to a script and also look at the way to access those arguments inside the script. Pa
      7 min read

    Input And Output

    • Shell Scripting - Standard Input, Output and Error
      Working on Linux applications we have several ways to get information from outside and to put it inside: command line args, environment variables, files. All of these sources are legal and good. But it has a finite size. Another way to establish communication is standard streams: input stream (stdin
      7 min read

    • Shell Script to Perform Operations on a File
      Most of the time, we use shell scripting to interact with the files. Shell scripting offers some operators as well as some commands to check and perform different properties and functionalities associated with the file. For our convenience, we create a file named 'geeks.txt' and another .sh file (or
      5 min read

    Command-Line Arguments and Options

    • Shell Script to Demonstrate Special Parameters With Example
      Here, we are going to see what are the special Parameters of the shell script. Before that first, let's understand what is parameters in the shell. The parameter is the entity that stores the value. The variables are the parameters that are defined by the user to use in that specific shell script. A
      5 min read

    Error Handling and Debugging

    • User Space Debugging Tools in Linux
      In this article, we will discuss debugging tools in Linux. Debugging Tools are those programs that allow us to monitor, control, and correct other program's error while they execute. Some of the debugging tools are as follows:- 'print Statement'Querying (/proc, /sys etc)Tracing (strace/ltrace)Valgri
      3 min read

    • Shell Scripting - System Logging
      Shell scripting is a way of automating tasks and operations on a computer system by writing scripts and programs in a shell or terminal environment. These scripts can run commands, access system resources, and process data. Shell scripts are often used to automate repetitive tasks, such as backups,
      9 min read

    Regular Expressions

    • How to Use Regular Expressions (RegEx) on Linux
      Regexps are acronyms for regular expressions. Regular expressions are special characters or sets of characters that help us to search for data and match the complex pattern. Regexps are most commonly used with the Linux commands:- grep, sed, tr, vi. The following are some basic regular expressions:
      5 min read

geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences