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

Bourne Shell (sh) in Linux Programming

Introduction of Bourne Shell

 The Bourne shell is a command-line interpreter and scripting language for computer operating systems. Developed by Stephen R. Bourne at AT&T Bell Laboratories, it was introduced in 1977 and officially released in 1979 as part of Version 7 Unix (V7). It served as the direct replacement for Ken Thompson's early shell, retaining the standard binary filename sh (located at /bin/sh).

 Although designed primarily as an interactive command interpreter, it was engineered with robust structured programming semantics. It introduced control flow structures, variable scoping, input/output file descriptor redirection, signal trapping, and error handling that became the international POSIX standard (IEEE Std 1003.1) for shell interfaces worldwide.

Linux Shell Types Evolution Diagram

Figure 1: Evolution of UNIX & Linux Shells originating from the Bourne Shell (sh)


1. Core Features of the Bourne Shell

Stephen Bourne's design established foundational capabilities that every system administrator and programmer relies upon today:

  • Filename Wildcards (Globbing): Support for * (matches any string), ? (matches a single character), and [...] (character range matching).
  • Input & Output Redirection: Full control of file descriptors 0 (stdin), 1 (stdout), and 2 (stderr) via <, >, >>, and 2>&1.
  • Structured Control Flow: Implementation of block keywords like if ... then ... else ... fi, case ... in ... esac, for, and while loops.
  • Here-Documents: Multi-line block inline input redirection via << DELIMITER.
  • Environment Scoping: Distinction between shell variables and exported environment variables passed to child processes with export.
  • Signal Interception: Catching and handling operating system signals (e.g. SIGINT, SIGTERM) using the built-in trap command.
  • Synchronous & Asynchronous Execution: Executing background processes concurrently using the trailing ampersand &.

2. Syntax & Working Script Example

In the Bourne Shell, conditional evaluations are performed using the test command or its bracket synonym [ ... ] with spaces around arguments:

#!/bin/sh
# =========================================================
# Script: check_days.sh
# Target Interpreter: POSIX Bourne Shell (/bin/sh)
# =========================================================

days=400

# Test condition: integer comparison -gt (greater than)
if [ "$days" -gt 365 ]; then
    echo "This is over a year."
else
    echo "This is less than or equal to a year."
fi

Output from running under /bin/sh:

$ chmod +x check_days.sh
$ ./check_days.sh
This is over a year.

3. Technical Comparison: Bourne Shell (sh) vs Bash

While Bash (Bourne-Again Shell) is backward-compatible with sh, the classic Bourne Shell lacked many modern features:

Feature Original Bourne Shell (sh) Modern Bash (bash)
Default Shell Prompt $ for users, # for root Rich prompt with colors & git info (PS1)
Arithmetic Expansion Requires external tool: `expr $a + 1` Built-in arithmetic: $(( a + 1 ))
Arrays No array support (only positional $1..$9) Indexed arrays & associative hash maps
Conditional Brackets POSIX [ ... ] only Extended test [[ ... ]] with regex matching
Interactive Usability No command history, no arrow-key editing Full GNU Readline (reverse search Ctrl+R, history)
Command Substitution Legacy backticks: `date` Nestable syntax: $(date)

4. What is /bin/sh on Modern Linux Systems?

If you inspect /bin/sh on modern Linux installations today, it is virtually always a symbolic link to a modern POSIX-compliant engine:

  • Debian & Ubuntu: /bin/sh -> /bin/dash. The Debian Almquist Shell (Dash) is an ultra-lightweight, high-speed POSIX shell optimized for system boot scripts.
  • Red Hat, Fedora & CentOS: /bin/sh -> /bin/bash. When Bash is invoked via the symlink name sh, it automatically disables non-POSIX extensions and conforms to standard POSIX behaviour.
  • Alpine Linux & Embedded: /bin/sh -> /bin/busybox. A multi-call binary providing a compact POSIX shell for containers.
$ ls -l /bin/sh
lrwxrwxrwx 1 root root 4 Mar 15 10:20 /bin/sh -> dash

5. Hands-on Lab Practice Drill

Lab Exercise: Write a 100% POSIX Portable Shell Script

Create a script named portable_audit.sh that runs without errors on classic Bourne shell, Dash, and Bash without relying on any "bashisms":

$ cat << 'EOF' > portable_audit.sh
#!/bin/sh
# -------------------------------------------------------------
# File: portable_audit.sh
# Purpose: Clean POSIX-compliant system inspection
# -------------------------------------------------------------

echo "=== System Audit (POSIX /bin/sh) ==="

# 1. Capture hostname and kernel
HOST=$(hostname)
KERNEL=$(uname -r)
echo "Node Name : $HOST"
echo "Kernel    : $KERNEL"

# 2. Portable loop over command-line arguments
count=0
for item in "$@"; do
    count=`expr $count + 1`
    echo "Argument $count : $item"
done

# 3. Check file existence portably
if [ -f "/etc/passwd" ]; then
    echo "Security check: /etc/passwd is present."
fi
EOF

$ chmod +x portable_audit.sh
$ ./portable_audit.sh Alpha Beta Gamma
=== System Audit (POSIX /bin/sh) ===
Node Name : veewom-server
Kernel    : 5.15.0-generic
Argument 1 : Alpha
Argument 2 : Beta
Argument 3 : Gamma
Security check: /etc/passwd is present.

6. University Viva & Job Interview Q&A

Q1: What does POSIX compliance mean in the context of Bourne Shell?

Answer: POSIX (Portable Operating System Interface) IEEE Std 1003.1 standardized the core features of the Bourne shell. Writing POSIX-compliant scripts ensures code runs seamlessly across Linux, AIX, HP-UX, Solaris, FreeBSD, and macOS without syntax errors.

Q2: Why do scripts fail when moving from Bash to Bourne/Dash?

Answer: Scripts fail if they contain "bashisms"—syntax specific to Bash such as [[ ... ]], == for string comparison, function foo() syntax, associative arrays, or the source command instead of ..

Q3: How was arithmetic performed in the original Bourne shell?

Answer: The original Bourne Shell lacked native math operators. Calculations had to be delegated to external command utilities like expr (e.g., result=`expr $a + $b`) or bc for floating-point calculations.


7. Frequently Asked Questions (FAQ)

Is Bourne Shell still in use today?

Yes. While modern interactive users prefer Bash or Zsh, Bourne-compatible POSIX shells (like Dash and BusyBox) power Linux OS boot sequences, Docker base images, and embedded IoT systems because of their speed and small footprint.

Should I write #!/bin/sh or #!/bin/bash in my shebang?

Use #!/bin/bash if you require Bash features like arrays, [[ ]], and process substitution. Use #!/bin/sh if you are authoring system daemon scripts or public libraries that must run portably on all UNIX/Linux systems.

Next Step in Your Linux Learning Path:

Explore Bill Joy's C-like shell syntax and interactive history expansion in Class 4: C Shell & TCSH.

Continue to Class 4: C Shell (csh) → Previous: ← Overview of Shell | 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!