Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

Introduction to Shell Programming

Lesson 6 of 32 in the free Design of Unix Operating System notes on Siksha Sarovar, written by Rohit Jangra.

Introduction

A Shell is a command-line interpreter that provides an interface between the user and the UNIX kernel. It reads commands typed by the user, interprets them, and passes them to the kernel for execution.

Shell programming (or shell scripting) involves writing a sequence of commands in a file that the shell can execute automatically.

Types of Shells in UNIX

ShellNamePathDescription
shBourne Shell/bin/shOriginal UNIX shell by Stephen Bourne
cshC Shell/bin/cshDeveloped at Berkeley, C-like syntax
kshKorn Shell/bin/kshCombines features of sh and csh
bashBourne Again Shell/bin/bashDefault on most Linux systems, enhanced sh
tcshTENEX C Shell/bin/tcshEnhanced C Shell with auto-completion
zshZ Shell/bin/zshModern shell with advanced features

Shell Script Basics

Creating a Shell Script:

  1. Create a file with .sh extension.
  2. Add the shebang line at the top: #!/bin/bash
  3. Write your commands.
  4. Make the file executable: chmod +x script.sh
  5. Run it: ./script.sh

Variables

  • Defining: NAME="Unix" (No spaces around =)
  • Accessing: echo $NAME
  • Read-only: readonly NAME
  • Unsetting: unset NAME
  • System Variables: $HOME, $PATH, $USER, $SHELL, $PWD

Input and Output

  • Reading Input: read -p "Enter name: " name
  • Printing Output: echo "Hello, $name"

Operators

Arithmetic Operators: +, -, *, /, % (used inside $(( )) or with expr)

Comparison Operators (for numbers):

  • -eq (equal), -ne (not equal)
  • -gt (greater than), -lt (less than)
  • -ge (greater or equal), -le (less or equal)

String Operators:

  • = (equal), != (not equal)
  • -z (string is empty), -n (string is not empty)

Control Structures

If-Else:

if [ condition ]; then
    commands
elif [ condition ]; then
    commands
else
    commands
fi

For Loop:

for var in list; do
    commands
done

While Loop:

while [ condition ]; do
    commands
done

Case Statement:

case $variable in
    pattern1) commands ;;
    pattern2) commands ;;
    *) default commands ;;
esac

Functions

function_name() {
    commands
    return value
}
# Calling
function_name

Important Concepts

1. Command Substitution:

  • result=$(command) — Stores command output in a variable.

2. Redirection:

  • > — Redirect output to a file (overwrite).
  • >> — Redirect output to a file (append).
  • < — Take input from a file.
  • 2> — Redirect error output.

3. Piping:

  • command1 | command2 — Pass output of command1 as input to command2.

Summary

  • A shell is a CLI that interprets user commands for the kernel.
  • Shell scripts automate repetitive tasks.
  • Key concepts include variables, control structures, functions, redirection, and piping.
  • Bash is the most widely used shell today.