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.
Category |
Keywords / Built-ins |
Purpose |
|---|---|---|
Conditionals |
|
Branching control structures for evaluating conditional tests. |
Multi-Way Branching |
|
Pattern-matching switch statement for menu-driven scripts. |
Looping Constructs |
|
Iterative structures for executing blocks repeatedly until conditions change. |
Loop Controls |
|
Terminates a loop early or skips immediately to the next iteration. |
Variable Attributes |
|
Defines constants, exports to subshells, and deletes variables. |
Standard I/O |
|
Prints formatted output to stdout and reads user input into variables. |
Process & Signal |
|
Signal interception, process replacement, subshell synchronization, and exit codes. |
System Resource Limits |
|
Inspects/sets CPU/file limits and sets default file permission creation masks. |
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
trap Keyword: Intercepting Interruptstrap 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
exec Keyword: Process Replacementexec 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"
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
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.
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