The Shell contributes a foundational role in the Linux operating system. At its core, the shell is a command interpreter that acts as an intermediary layer between the user (or software programs) and the operating system kernel. When you type commands at the shell prompt, the shell parses the syntax, locates the corresponding executable binary, sets up file descriptors, and instructs the kernel to execute it.
While entering commands interactively in terminal mode is convenient for quick tasks, automating repetitive daily maintenance (such as taking database backups, rotating server logs, compiling software builds, or monitoring CPU usage) requires writing a Shell Script.
A Shell Script is an executable plain text file containing a sequence of commands executed line-by-line by a shell interpreter without requiring compilation. The shell manages three default standard I/O streams for every executing script: Standard Input (stdin, FD 0), Standard Output (stdout, FD 1), and Standard Error (stderr, FD 2).
Figure 1: Complete 5-step lifecycle of a Linux shell script: Write, Shebang, Chmod, Execute, Exit Code.
Different shell programs are available in Linux, each offering unique scripting features and command syntaxes:
Sr.No |
Shell Name & Binary |
Developer & Year |
Primary Characteristics |
|---|---|---|---|
01 |
Bourne Shell ( |
Stephen Bourne (1979) |
The original Unix shell. Standardized by POSIX; compact, lightweight, universal baseline for portable scripts. |
02 |
C Shell ( |
Bill Joy (1978) |
Designed with C-like syntax and introduced command history and job control. Popular in academic computing. |
03 |
Korn Shell ( |
David Korn (1983) |
Backward compatible with Bourne shell while introducing associative arrays and built-in floating-point arithmetic. |
04 |
Bash Shell ( |
Brian Fox & Chet Ramey (1989) |
Bourne Again SHell: The default shell on virtually all Linux distributions. Combines sh compatibility with modern enhancements. |
Every production shell script consists of five fundamental components:
#!/bin/bash): Must be the absolute first line. It tells the Linux kernel which interpreter executable to launch.# Description): Documenting what the script does so colleagues and system administrators understand its behavior.if-else), loops (for/while), and native Linux utilities (grep, awk, tar).exit 0): Signals to the parent process or scheduling cron job whether execution succeeded (0) or encountered an error (non-zero).Create a practical system reporting script in your terminal:
#!/bin/bash
# system_report.sh - Automated Linux Health Summary
# Author: Veewom Linux Programming Class
echo "=================================================="
echo " LINUX SYSTEM STATUS REPORT "
echo "=================================================="
echo "Generated On : $(date)"
echo "Current User : $USER"
echo "System Hostname: $(hostname)"
echo "Kernel Release : $(uname -r)"
echo "Uptime Summary : $(uptime -p)"
echo "--------------------------------------------------"
echo "Memory Usage (MB):"
free -m | awk 'NR==2{printf "Used: %s MB | Free: %s MB | Total: %s MB\n", $3, $4, $2}'
echo "--------------------------------------------------"
echo "Root Disk Usage:"
df -h / | awk 'NR==2{printf "Used: %s | Free: %s | Utilization: %s\n", $3, $4, $5}'
echo "=================================================="
exit 0
To run this script:
$ chmod +x system_report.sh
$ ./system_report.sh
#! indicate to the Linux kernel that the rest of the line is the path to the interpreter program that must execute the script file.
chmod +x filename.sh grants execute permission, allowing the kernel to run the script directly with ./filename.sh.
Learn shebang syntax, chmod permissions, and execution methods in Class 13: How to Write and Run Scripts.
Continue to Class 13: How to Write & Run Scripts → Previous: ← VI Commands | Full Syllabus