← All guides

Programmer’s Reference

Bash / Linux

Navigate, inspect files, write scripts, and diagnose a Linux host.

Bash on Linux, with GNU tools. macOS and other Unix systems may use different flags. Replace demo paths before use.

19 entries · Essentials first · Reviewed October 5, 2026

Glossary: commands, keywords & terms

Select a term for its definition, syntax, example, and common mistakes.

Keywords and syntax

if / then / elif / else / fi

Branch on command exit status: zero means success.

Bash script
#!/usr/bin/env bash
count=3
if (( count > 5 )); then
  printf '%s\n' Many
elif (( count > 0 )); then
  printf '%s\n' Some
else
  printf '%s\n' None
fi

Practical useUse this construct in your own practice project.

Common mistakesBash tests exit status, not printed text; close the construct with fi.

Source: www.gnu.org documentation

|| (OR list)

Run the next command only if the previous command fails.

Bash script
#!/usr/bin/env bash
test -d ./demo-project || printf '%s\n' 'Directory missing'

Practical useUse this construct in your own practice project.

Common mistakesFailure means a nonzero exit status; || does not inspect output.

Source: www.gnu.org documentation

&& (AND list)

Run the next command only if the previous command succeeds.

Bash script
#!/usr/bin/env bash
test -d ./demo-project && printf '%s\n' 'Directory exists'

Practical useUse this construct in your own practice project.

Common mistakesChains are not always equivalent to an if/else block.

Source: www.gnu.org documentation

for / in / do / done

Loop through a list of words.

Bash script
#!/usr/bin/env bash
for name in Demo Example; do
  printf '%s\n' "$name"
done

Practical useUse this construct in your own practice project.

Common mistakesQuote variable expansions to prevent splitting and globbing.

Source: www.gnu.org documentation

while / do / done

Repeat while the condition command succeeds.

Bash script
#!/usr/bin/env bash
count=0
while (( count < 3 )); do
  printf '%s\n' "$count"
  count=$((count + 1))
done

Practical useUse this construct in your own practice project.

Common mistakesMake the condition eventually fail.

Source: www.gnu.org documentation

case / esac

Choose a branch by matching a shell pattern.

Bash script
#!/usr/bin/env bash
state=ready
case "$state" in
  ready) printf '%s\n' Ready ;;
  *) printf '%s\n' Unknown ;;
esac

Practical useUse this construct in your own practice project.

Common mistakesPatterns are shell patterns, not regular expressions; close cases with ;;.

Source: www.gnu.org documentation

function / return

Define a reusable shell function and return an exit status.

Bash script
#!/usr/bin/env bash
describe() {
  printf '%s\n' "$1"
  return 0
}
describe Demo

Practical useUse this construct in your own practice project.

Common mistakesreturn sets status, not an arbitrary string result; use stdout for text.

Source: www.gnu.org documentation

break / continue

Leave a loop or skip to its next iteration.

Bash script
#!/usr/bin/env bash
for n in 1 2 3 4; do
  if (( n == 2 )); then continue; fi
  if (( n == 4 )); then break; fi
  printf '%s\n' "$n"
done

Practical useUse this construct in your own practice project.

Common mistakesThese builtins need an enclosing loop.

Source: www.gnu.org documentation

goto (not supported)

Bash has no goto builtin or label-based jump syntax; use loops and functions.

Bash script
#!/usr/bin/env bash
describe() {
  if [[ $1 == ready ]]; then
    printf '%s\n' Ready
    return 0
  fi
  printf '%s\n' Waiting
}
describe ready

Practical useUse this construct in your own practice project.

Common mistakesA word named goto is treated as a command name, not a language keyword.

Source: www.gnu.org documentation

Essentials

Variables and quoting

Use double quotes to keep a variable value as one argument.

Bash script
#!/usr/bin/env bash
site='Demo site'
printf '%s\n' "$site"

Practical useKeep a site label containing spaces intact.

Common mistakesUnquoted expansions can split words and expand wildcards. Single quotes prevent variable expansion.

Source: www.gnu.org documentation

Files and processes

Pipes and redirects

Send one command’s output into another and save a new report.

⚠ Caution: The > redirect replaces existing file contents.

Terminal
printf "%s\n" "demo: running" | sort
printf "%s\n" "demo: running" > demo-report.txt

Practical useWrite a disposable health summary in a staging directory.

Common mistakes> truncates an existing file; >> appends. A pipe normally reports the last command’s exit status.

Source: www.gnu.org documentation

Copy and create directories

Create a backup directory and copy a demo file while preserving basic metadata.

⚠ Caution: May overwrite the destination file.

Terminal
mkdir -p demo-backups
cp -p -- demo.conf demo-backups/demo.conf

Practical useBack up a staging configuration before editing.

Common mistakescp can overwrite an existing destination. A file copy is not a consistent database backup.

Source: www.gnu.org documentation (1) · www.gnu.org documentation (2)

Inspect and set file permissions

Inspect ownership and grant the owner read/write access to a private demo file.

⚠ Caution: Changes file access; a service using the file may lose access.

Terminal
ls -l demo.env
chmod 600 demo.env

Practical useKeep a local environment fixture readable only by its owner.

Common mistakesPermissions alone do not fix ownership, ACLs, or SELinux. Never use chmod 777 as a general repair.

Source: www.gnu.org documentation

Find files by name

Search a demo tree for configuration files without modifying them.

Terminal
find ./demo-project -type f -name '*.conf' -print

Practical useInventory configuration files in a staging copy.

Common mistakesQuote the wildcard so the shell does not expand it first. Confirm the root directory.

Source: www.gnu.org documentation

Remove one demo file

Request confirmation before unlinking one explicitly named file.

⚠ Caution: Deletes the named file; ensure a recoverable copy exists.

Terminal
rm -i -- ./demo-output.txt

Practical useClean up a disposable report after reviewing it.

Common mistakesDeleted files generally bypass a trash bin. Do not add -r or -f casually, or use an unverified variable as the path.

Source: www.gnu.org documentation

Scripts and troubleshooting

Syntax and exit checks

Check a Bash script’s syntax without running its commands.

Terminal
bash -n deploy-demo.sh
printf "Exit status: %s\n" "$?"

Practical useValidate a release helper before staging execution.

Common mistakesSyntax checks do not prove a script is safe. Read the script and test it on disposable staging data.

Source: www.gnu.org documentation

Back to top ↑