bash
20 pages
-
bash parse arguments with getopts and $@
Parse command-line arguments in bash scripts using getopts for flags, positional parameters ($1, $2), and shift for more control.
-
bash arrays — create, iterate, append, slice
Create and use bash arrays: declare, iterate with for, append elements, get length, slice, and use associative arrays.
-
bash check exit code of last command — $?
Check the exit code of the last bash command with $?. Exit code 0 means success; anything else means failure.
-
bash check if file or directory exists
Test if a file, directory, or symlink exists in bash using [ -f ], [ -d ], [ -e ], and [ -L ] in an if statement or inline.
-
bash: command not found — how to fix
Fix "bash: command not found" by checking PATH, installing the missing program, or using the full path to the binary.
-
crontab syntax and common schedules
Crontab syntax reference with common schedule examples. Covers the five time fields, special strings (@daily, @reboot), and editing cron jobs.
-
bash date — format date and timestamp
Format dates and timestamps in bash using the date command. Covers ISO 8601, Unix epoch, arithmetic, and macOS vs Linux differences.
-
bash find files by name, extension, or age
Use the find command to locate files by name, extension, size, modification date, or content. Includes -exec, -mtime, and -type options.
-
bash for loop — iterate over files, list, range
Use bash for loops to iterate over files, lists, arrays, ranges, and command output. Covers glob patterns, C-style loops, and while loops.
-
bash heredoc — write multiline strings
Use heredoc (<<EOF) to embed multiline text in bash scripts. Covers indented heredocs (<<-), variable expansion, and writing to files.
-
bash kill process by port number
Kill the process listening on a specific port using lsof, fuser, or ss. Works on Linux and macOS.
-
bash: no such file or directory — how to fix
Fix "bash: no such file or directory" caused by wrong path, missing file, Windows line endings (CRLF), or a missing interpreter.
-
bash permission denied — how to fix (chmod +x)
Fix "bash: ./script.sh: Permission denied" by adding execute permission with chmod +x. Covers common permission issues and setuid.
-
bash set -o pipefail — catch errors in pipelines
Use set -o pipefail to make bash pipelines fail when any command in the pipeline fails, not just the last one.
-
bash read a file line by line
Read a file line by line in bash using a while read loop. Handles lines with spaces, special characters, and trailing newlines correctly.
-
bash redirect stderr to stdout — 2>&1 and &>
Redirect stderr (2) to stdout (1) in bash using 2>&1. Includes capturing both streams, redirecting to a file, and suppressing output.
-
bash set -e — exit script on error
Use set -e (errexit) to make a bash script exit immediately when any command fails. Covers caveats, set -u, and set -o pipefail.
-
bash string comparison — equality, contains, starts with
Compare strings in bash using [[ ]] with =, !=, =~, and parameter expansion. Covers equality, contains, starts with, ends with, and empty checks.
-
bash syntax error: unexpected end of file — how to fix
Fix "syntax error: unexpected end of file" in bash scripts caused by unclosed if/for/while, missing fi/done/esac, or CRLF line endings.
-
bash xargs — run a command for each input line
Use xargs to pass stdin lines as arguments to a command. Covers -I, -P for parallel execution, null-delimited input, and safe filenames with spaces.