Class 14 of 37 • Shell Scripting Fundamentals \t\t\t\t

Shell Keywords in Linux - Reserved Words, Builtins & Syntax Rules

In Linux shell scripting, Keywords (also known as Reserved Words) are tokens with special grammatical meaning hardcoded into the shell's parser. The shell uses them to structure program logic, conditional branches, loops, and pipeline control. Keywords cannot be used as variable names or function names, as attempting to do so causes immediate syntax interpretation errors.

Master Categorized Table of Shell Keywords & Builtins

Category

Keywords / Built-ins

Purpose

Conditionals

if, then, else, elif, fi

Branching control structures for evaluating conditional tests.

Multi-Way Branching

case, in, esac

Pattern-matching switch statement for menu-driven scripts.

Looping Constructs

for, while, until, do, done

Iterative structures for executing blocks repeatedly until conditions change.

Loop Controls

break, continue

Terminates a loop early or skips immediately to the next iteration.

Variable Attributes

readonly, export, set, unset

Defines constants, exports to subshells, and deletes variables.

Standard I/O

echo, read

Prints formatted output to stdout and reads user input into variables.

Process & Signal

trap, wait, exec, exit, return

Signal interception, process replacement, subshell synchronization, and exit codes.

System Resource Limits

ulimit, umask

Inspects/sets CPU/file limits and sets default file permission creation masks.

How to Differentiate: Keyword vs Built-in vs Binary

In Linux, commands come in three distinct types. You can inspect any command using the built-in type utility:

$ type if
if is a shell keyword

$ type cd
cd is a shell builtin

$ type ls
ls is /usr/bin/ls

Special Advanced Keywords in Action

1. The trap Keyword: Intercepting Interrupts

trap enables scripts to catch unexpected interruptions (like Ctrl+C) and clean up temporary files before exiting:

#!/bin/bash
# Clean up temporary scratch file on exit or interrupt
tempfile="/tmp/scratch_$$.tmp"
touch "$tempfile"

# Catch SIGINT (Ctrl+C) and normal EXIT
trap 'echo "Cleaning up $tempfile..."; rm -f "$tempfile"; exit' INT EXIT

echo "Working with tempfile: $tempfile (Press Ctrl+C to test trap)"
sleep 10

2. The exec Keyword: Process Replacement

exec replaces the currently running shell process entirely with the target command without creating a child subshell:

#!/bin/bash
echo "Current Shell PID: $$"
# Replaces this script process with htop
exec htop
# This line is never reached because the script process was replaced!
echo "This will never print"

Lab Practice: Demonstrating Keyword Flow Control & Exit Status

Write a script that validates system permissions using keywords:

#!/bin/bash
# keyword_demo.sh

echo "Checking execution permissions for user: $USER"

# Keywords: if, then, else, fi, exit
if [ "$EUID" -ne 0 ]; then
    echo "Notice: Non-root user detected. Running in standard unprivileged mode."
else
    echo "Warning: Running as ROOT superuser!"
fi

# Keywords: for, in, do, done, break
echo "Checking critical system files:"
for file in "/etc/passwd" "/etc/shadow" "/etc/hosts"; do
    if [ -r "$file" ]; then
        echo "  [OK] $file is readable."
    else
        echo "  [DENIED] $file cannot be read."
    fi
done

exit 0

Frequently Asked Questions (FAQs)

No. Shell keywords are reserved for parsing syntax. For example, writing for=10 or if="true" causes syntax parsing conflicts and script failure.
break immediately terminates the entire enclosing loop and resumes execution after done. continue skips only the remainder of the current iteration and jumps to the next cycle.
exit terminates the entire shell script or subshell process and returns a status code to the parent shell. return exits only from an individual shell function back to the calling point inside the script.

Next Step in Your Linux Learning Path:

Document scripts cleanly using single-line, inline, and heredoc multi-line comments in Class 15: Shell Comments.

Continue to Class 15: Shell Comments → Previous: ← Write Script | 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!