It does not fire for commands whose exit status is tested (e.g., if ! command; then). If you need Fine-grained error handling, use explicit error checking with $? or trap.
echo " Cleaning up... (exit code: $exit_code ) "
trap ' echo "Received SIGINT"; exit 130 ' INT
trap ' echo "Received SIGTERM"; exit 143 ' TERM
echo " Working in $TMPDIR "
files = $( find /etc -name " *.conf " -type f )
count = $( wc -l < /etc/passwd )
# Legacy form (avoid — nesting is painful)
files = ` find /etc -name " *.conf " -type f`
base_dir = $( basename $( dirname $( realpath $0 )))
# Mapfile — read command output into an array (bash 4.0+)
mapfile -t lines < <( ps aux )
echo " First line: ${ lines [0]} "
echo " Total lines: ${ # lines [ @ ]} "
# WRONG — word splitting breaks on spaces
for file in $( ls * .txt ); do
# CORRECT — glob expansion preserves spaces within each match
# $* expands to a single word: "a b c"
# $@ expands to separate words: "a" "b" "c"
# Always quote $@ when forwarding arguments
# This will NOT cause the script to exit:
# Because only the last command's exit status matters.
false | true # NOW the script exits
# If $1 starts with a dash, it gets interpreted as an option
rm $1 # DANGEROUS if $1 is "-rf /"
# Always use -- to signal end of options
# If the directory does not exist, the script continues in the wrong directory!
cd /some/path # exits immediately on failure
# WRONG — for reads words, not lines
for line in $( cat file.txt ); do
# CORRECT — while read processes line by line
while IFS = read -r line ; do
# Also handle the last line without trailing newline
while IFS = read -r line || [[ -n " $line " ]]; do
Scripts using [[Arrays, (( ))Or local must use #!/bin/bash. On Debian/Ubuntu, /bin/sh Is dashWhich does not support these. The script will fail with syntax errors that are hard to Diagnose because the error messages come from dash, not bash.
# WRONG — if $VAR is empty, this becomes [ -f ], which is true
# CORRECT — handles empty variables correctly
# BEST — use [[ ]] which has no word splitting
# WRONG — leading tabs are preserved in the output
# CORRECT — <<- strips leading tabs (only tabs, not spaces)
indented text (tabs stripped)
# CORRECT — use IFS to strip whitespace
while IFS = $' \t ' read -r line ; do
echo " ${ line # " ${ line %% [ ! [ : space : ]] * } " } "
$( command_with_indented_output )
echo behavior varies between implementations. The -e flag (interpret escape sequences) is Supported by bash’s built-in echo but not by the POSIX echo from /bin/echo on some systems. The -n flag (suppress trailing newline) is also non-portable.
# Use printf instead — it is POSIX and consistent
printf ' %s\n ' " hello world "
printf ' %-20s %5d\n ' " filename " 42
printf ' Error: %s (code %d)\n ' " $msg " " $code "
printf interprets C-style format specifiers (%s``%d``%f``%x``%o) and is the recommended Way to produce formatted output in shell scripts.
This topic covers the core concepts of shell basics, including underlying theory, practical implementation, and key applications.
Key concepts include:
Git fundamentals (add, commit, push, pull) branching and merging strategies resolving merge conflicts rebasing and cherry-picking Git workflows (GitFlow, trunk-based) Understanding these concepts thoroughly is essential for both examinations and practical programming, and requires both theoretical knowledge and hands-on practice.
The shell is like a translator between you and the operating system — you speak human (commands), the kernel speaks machine (syscalls), and the shell bridges the gap. Bash is the most common translator, but zsh and dash are alternatives with different strengths. The shell’s prompt (PS1) is like a conversation indicator — it tells you who you are, where you are, and what the shell is ready to do. Environment variables are like the shell’s memory — they store settings (PATH, HOME) that affect how commands run. Understanding the shell means understanding how Linux works at the most fundamental level.
Worked examples demonstrating the application of key concepts are covered in the detailed sub-pages linked above.
Bash Scripting — Shell variables, control flow, and functions are the building blocks of bash scripts.I/O Redirection — Shell features like pipes and redirection are fundamental to command composition.Core Utilities — Shell commands invoke core utilities; understanding the shell enhances command-line efficiency.Processes and Signals — The shell creates and manages processes; background jobs and signals are shell-level concepts.