name='alice'Do not put spaces around `=` in shell variable assignments.
High-value Linux shell scripting reference covering variables, expansion, conditionals, loops, functions, redirection, pipelines, traps, getopts, and script debugging.
Define shell variables, expand strings, and work safely with parameters.
name='alice'Do not put spaces around `=` in shell variable assignments.
export APP_ENV=productionUsed for config values consumed by child commands and scripts.
Expand parameter with a fallback.
echo "${PORT:-8080}"Useful in scripts where environment variables may be optional.
Fail fast if a variable is missing.
echo "${DATABASE_URL:?DATABASE_URL is required}"Great for scripts that should stop immediately when config is missing.
files=$(find . -type f | wc -l)Use `$(...)` instead of legacy backticks.
printf '%s
' "$name"Double-quote variable expansions unless you intentionally want word splitting.
Build shell scripts that branch, loop, and encapsulate logic.
if [[ -f /etc/app.conf ]]; then echo 'exists'; fiCommon script pattern for optional config files.
if [[ -d releases ]]; then ls releases; fiUseful for deployment and housekeeping scripts.
for f in *.log; do echo "$f"; doneQuote variables inside the loop body to handle spaces safely.
while IFS= read -r line; do echo "$line"; done < input.txtPreferred pattern for preserving whitespace and backslashes.
case "$1" in start) echo start ;; stop) echo stop ;; *) echo usage ;; esacCleaner than long `if/elif` chains for command dispatch.
greet() { echo "hello $1"; }Functions help keep scripts readable and avoid repetition.
set -euo pipefailWidely used hardening pattern for production shell scripts.
trap 'rm -f "$tmpfile"' EXITUseful for temporary files, locks, and service teardown in scripts.
Redirect streams, build pipelines, and control output files.
echo 'hello' > greeting.txtUse `>>` to append instead of overwrite.
echo 'next line' >> app.logCommon for quick logging in scripts.
command 2> errors.logUseful when preserving errors separately from normal output.
command > command.log 2>&1Common logging pattern in scripts and cron jobs.
Send output of one command as input to another.
ps aux | grep nginxPipelines are the foundation of Unix composition.
Pass command output as a pseudo-file.
diff <(sort old.txt) <(sort new.txt)Handy when a command expects file arguments but you want to compare pipeline output.
read -rp 'Enter environment: ' envUseful for operator-driven scripts and local helpers.
Use printf instead of echo for predictable formatting.
printf 'user=%s id=%s
' "$USER" "$UID"Preferred in scripts because format control is explicit.
Run scripts, parse arguments, and debug shell behavior.
#!/usr/bin/env bashPortable way to locate Bash on many systems.
chmod +x deploy.shThen run it directly as `./deploy.sh`.
./deploy.shUse an explicit path so the shell does not rely on `PATH`.
bash -x ./deploy.shVery useful for debugging shell scripts and CI tasks.
while getopts ':f:n:' opt; do case "$opt" in f) file="$OPTARG" ;; n) count="$OPTARG" ;; esac; doneUseful for small production scripts with predictable CLI flags.
shellcheck deploy.shOne of the highest-value tools for improving script quality.