Class 2 of 37 • Linux Shell Essentials \t\t\t\t

Overview of Shell in Linux Programming - Architecture, Types & Execution

 A shell script is a computer program designed to be run by the Unix shell, a command-line interpreter.The various dialects of shell scripts are considered to be scripting languages.

 Typical operations performed by shell scripts include file manipulation, program execution, and printing text. A script which sets up the environment, runs the program, and does any necessary cleanup, logging, &c is called a wrapper.

 The shell provides you with an interface to the UNIX system.It gathers input from you and executes programs based on that input.when a program finishes executing it displays that program's output.

 Shell script is series of commands written in plain text.file.shell script is just like batch file in MS-DOS but have more power than the ms-dos batch file.

 Normally shells are interactive.it means shell accept command from you and execute them.but if you use command one by one,then you can store this sequence of commands to text file and tell the shell to execute this text file instead of entering the commands.this known as shell script.


How the Linux Shell Operates: Internal Execution Cycle

Whenever you type a command in the terminal prompt and hit Enter, the shell performs an intricate 6-step loop known as the REPL (Read-Eval-Print Loop):

     1. Prompt Display: Shell prints the prompt (defined by the $PS1 environment variable) and waits for user input.

     2. Parsing & Tokenization: The command string is split into tokens (command name, arguments, flags) based on whitespace delimiters.

     3. Expansions: The shell evaluates brace expansions {1..5}, tilde expansions ~/, parameter expansions $VAR, command substitutions $(cmd), and filename globbing *.txt.

     4. Command Resolution: It checks if the command is a shell built-in (e.g., cd, echo, pwd) or an external executable located in the directories listed under $PATH.

     5. Process Creation (Fork & Exec): For external commands, the shell calls the fork() system call to create a child process, followed by execve() to load and run the program.

     6. Status Collection: The shell waits for completion via waitpid() and stores the termination exit status code in the special variable $?.


Major Types of Linux Shells

Over the history of Unix and Linux, several shells were developed to optimize interactivity, scripting capability, and programming paradigms. Here is the comparative breakdown:

Types of Linux Shells: Bourne, Csh, Korn, Bash, and Zsh Comparison

Figure 2: Evolution and comparison of popular Unix/Linux shells.

Shell Name Command Default Prompt Core Distinction
Bourne Shell /bin/sh $ Original standard Unix shell created by Stephen Bourne at Bell Labs (1977).
C Shell /bin/csh % Designed with C-like syntax and history mechanism by Bill Joy (BSD Unix).
Korn Shell /bin/ksh $ Authored by David Korn; combines sh compatibility with Csh interactive power.
Bash Shell /bin/bash $ (or # for root) The GNU Bourne-Again Shell; the official default shell for Linux systems worldwide.
Z Shell /bin/zsh % Highly customizable interactive shell featuring advanced tab-completion and plugin frameworks.

Beginner Student Lab Practice Session

Open your terminal and execute the following commands to investigate the shell configuration of your machine:


# Task 1: Print your currently active login shell
$ echo $SHELL
/bin/bash

# Task 2: View all valid login shells available on this operating system
$ cat /etc/shells
/bin/sh
/bin/bash
/usr/bin/bash
/bin/rbash
/bin/dash

# Task 3: Check which shell is configured for your current username in passwd
$ grep "^$USER:" /etc/passwd

# Task 4: View your current terminal session line
$ tty
/dev/pts/0

# Task 5: Launch a subshell and verify process ID
$ echo "Parent PID: $$"
$ bash
$ echo "Subshell PID: $$"
$ exit
							

Frequently Asked Questions (FAQ)

The Kernel is the core software running in privileged mode (Ring 0) that directly controls the physical hardware (CPU, RAM, storage devices). The Shell is an outer user-space program (Ring 3) that provides a user-friendly interface to issue requests to the kernel without having direct dangerous access to hardware memory.

You can change your default login shell using the chsh (change shell) command followed by the -s option and the absolute path of the target shell (e.g., chsh -s /bin/bash).

Next Step in Your Linux Learning Path:

Discover the historic foundation of Unix shells in Class 3: Stephen Bourne's classic /bin/sh and POSIX compliance.

Continue to Class 3: Bourne Shell (sh) → Previous: ← Course Overview | Full Syllabus

Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Stumbleupon Share on Tumblr Share on Reddit Share on Diggit

You may also like this!