๐Ÿš Bash Dictionary

Every keyword, operator, and symbol โ€” with plain-English explanations.

$# variable
The number of positional arguments passed.
if [ $# -eq 0 ]; then echo no args; fi
$(command) expansion
Command substitution: runs command and inserts its output.
TS=$(date +%Y%m%d)
$0 variable
The name of the current script.
echo "Script: $0"
$1 variable
First positional argument passed to the script or function.
echo "Hello, $1"
$? variable
Exit code of the last command. 0 = success, non-zero = failure.
cmd; if [ $? -ne 0 ]; then echo failed; fi
$@ variable
All positional arguments as separate quoted strings.
for arg in "$@"; do process "$arg"; done
${VAR:-default} expansion
Uses default value if VAR is unset or empty.
PORT=${PORT:-8080}
${VAR:?error} expansion
Exits with error message if VAR is unset or empty.
DB=${DATABASE_URL:?DATABASE_URL required}
-d flag
Test flag: true if path exists and is a directory.
[ -d /tmp/out ] || mkdir /tmp/out
-eq operator
Numeric equality test inside [ ].
[ $count -eq 0 ]
-f flag
Test flag: true if path exists and is a regular file.
[ -f config.yml ] && source config.yml
-gt operator
Numeric greater-than test inside [ ].
[ $x -gt 0 ]
-lt operator
Numeric less-than test inside [ ].
[ $x -lt 10 ]
-n flag
Test flag: true if the string is non-empty.
[ -n "$VAR" ] && echo set
-ne operator
Numeric not-equal test inside [ ].
[ $x -ne $y ]
-z flag
Test flag: true if the string is empty (zero length).
[ -z "$VAR" ] && echo empty
/dev/null special
Discards all output written to it. Reading from it returns EOF immediately.
cmd > /dev/null 2>&1
2> redirect
Redirects stderr to a file.
cmd 2> errors.txt
2>&1 redirect
Merges stderr into stdout so both go to the same place.
cmd > all.log 2>&1
[ ] test
The test command. Evaluates conditions. Requires spaces inside brackets.
[ -f file.txt ] && echo exists
[[ ]] test
Extended test command. Supports regex, no word splitting, safer for strings.
[[ $s == *.go ]] && echo 'Go file'
alias BUILTINS
Create a shorthand for a command or sequence of commands.
alias ls='ls --color=auto'
awk command
Text processing tool for column-based data and pattern/action rules.
awk '{print $2}' data.txt
bg BUILTINS
Run the last job in the background.
jobs -l; bg %1
bind BUILTINS
Bind a command or sequence of commands to a key sequence.
bind '"'"'"\C-xl"'"'"' "ls --color=auto"
break BUILTINS
Exit the innermost for or while loop.
for i in {1..10}; do if [ $i -gt 5 ]; then break; fi; echo $i; done
builtin BUILTINS
Execute the specified shell built-in command.
builtin cd ~"
caller BUILTINS
Print a stack backtrace of the function call.
caller
case keyword
Matches a value against patterns, running the first matching block.
case "$1" in start) start_svc ;; stop) stop_svc ;; esac
cat command
Outputs file contents to stdout. Also concatenates multiple files.
cat file.txt
cd BUILTINS
Change the current working directory.
cd /tmp
chmod command
Changes file permissions. 755 = rwxr-xr-x, 644 = rw-r--r--.
chmod +x script.sh
chown command
Changes file owner and optionally group.
chown -R www-data:www-data /var/www
command BUILTINS
Run a command, suppressing shell function lookup.
command ls -l
compgen BUILTINS
Generate possible command completion matches.
compgen -d
complete BUILTINS
Define command completion behavior for a command.
complete -F _myfunc mycommand
compopt BUILTINS
Modify the command completion options.
compopt -o nospace
continue BUILTINS
Continue the next iteration of a for or while loop.
for i in {1..10}; do if [ $i -gt 5 ]; then continue; fi; echo $i; done
cp command
Copies files or directories. -r for recursive directory copy.
cp -r src/ dst/
declare BUILTINS
Declares shell variables and functions.
declare -i count=0
dirs BUILTINS
Displays or changes the directory stack.
dirs -v
disown BUILTINS
Remove a job from the shell's job table, typically to allow it to continue running after the shell exits.
disown %1
do keyword
Opens the body of a for, while, or until loop.
for x in a b c; do echo $x; done
done keyword
Closes a for, while, or until loop.
for f in *; do ...; done
elif keyword
An additional condition check inside an if statement.
if [ $x -eq 1 ]; then :; elif [ $x -eq 2 ]; then :; fi
else keyword
The fallback block when an if condition fails.
if false; then :; else echo no; fi
enable BUILTINS
Toggle shell function or shell built-in execution.
enable -p echo
esac keyword
Closes a case block (case spelled backwards).
case $x in 1) echo one ;; esac
eval BUILTINS
Evaluate a string as shell commands or arguments.
eval $(command)
exec BUILTINS
Replace the shell with the specified command.
exec /bin/bash
exit builtin
Terminates the script with an optional exit code (0 = success).
exit 1
export builtin
Marks a variable to be passed into child process environments.
export PATH="/usr/local/bin:$PATH"
false BUILTINS
Always return a false (non-zero) exit status.
false
fc BUILTINS
History expansion and editing, and execute a previous command or command line.
fc -s 100
fg BUILTINS
Bring a background job to the foreground.
fg %1
fi keyword
Closes an if/elif/else block (if spelled backwards).
if true; then echo y; fi
for keyword
Iterates over a list of words or a numeric range.
for f in *.sh; do chmod +x "$f"; done
function keyword
Declares a reusable function.
function hello() { echo "hi"; }
getopts BUILTINS
Parse command line arguments in a script.
while getopts ab:c opt; do case "$opt" in a) echo "Option a" ;; b) echo "Option b: $OPTARG" ;; c) echo "Option c" ;; esac done
hash BUILTINS
Cache the full path of the specified command in the hash table.
hash -r
head command
Outputs the first N lines of a file (default 10).
head -20 log.txt
help BUILTINS
Display help for a command.
help ls
heredoc syntax
Multi-line string that feeds content to a command via stdin.
cat <<EOF\nline 1\nline 2\nEOF
history BUILTINS
Manipulate the history list.
history -c
if keyword
Runs a block when the test command returns 0 (success).
if [ -f file ]; then echo exists; fi
jobs BUILTINS
List the active jobs.
jobs -l
kill BUILTINS
Send a signal to a process or a job.
kill %1
let BUILTINS
Evaluates the arguments as arithmetic expressions and prints the results.
let count=$count+1
local keyword
Limits a variable's scope to the current function.
local tmp=$(mktemp)
logout BUILTINS
Terminates the current shell session.
logout 
mapfile BUILTINS
Reads lines from a file and stores them in an array variable.
mapfile -d '' -t -s 1 -n 5 < /etc/passwd
mkdir command
Creates a directory. -p creates parents and doesn't fail if exists.
mkdir -p /opt/app/data
mv command
Moves or renames files and directories.
mv old.txt new.txt
popd BUILTINS
Remove the top entry from the directory stack and change to the new top entry.
popd -v
printf builtin
Prints formatted text without an automatic newline. More powerful than echo.
printf '%s\n' "$line"
pushd BUILTINS
Push the current directory onto the directory stack and change to the new directory.
pushd /tmp
pwd BUILTINS
Print the current working directory.
pwd
read builtin
Reads a line from stdin into a variable.
read -r line < file.txt
readarray BUILTINS
Read lines from a file or command output into an array variable, similar to mapfile.
readarray -t array < /etc/passwd
readonly BUILTINS
Mark variables as readonly, meaning they cannot be modified or unset.
readonly x=10
return keyword
Exits a function with an optional return code.
return 0
rm command
Removes files. -r for directories, -f to skip confirmation.
rm -rf /tmp/build
scp command
Copies files to/from a remote host over SSH.
scp file.txt user@host:/path/
sed command
Stream editor: transforms text using patterns and substitutions.
sed 's/old/new/g' file
set -o pipefail builtin
A pipeline fails if any command in it fails, not just the last one.
set -euo pipefail
shift BUILTINS
Shifts the positional parameters to the left, causing the first one to become the new $1, the second to become the new $2, and so on.
shift 2
shopt BUILTINS
Gets or sets the value of a shell option.
shopt -s expand_aliases 
sort command
Sorts lines of text. -n for numeric, -r for reverse, -u for unique.
sort -u names.txt
source builtin
Reads and executes a script file in the current shell session.
source ~/.bashrc
ssh command
Securely connects to a remote host over SSH.
ssh -i key.pem user@host
suspend BUILTINS
Sends a SIGTSTP signal to the current job or to a specified job.
suspend 
tail command
Outputs the last N lines. -f follows new output as it is written.
tail -f /var/log/syslog
tee command
Reads stdin and writes to both stdout and a file simultaneously.
cmd | tee output.txt
test BUILTINS
Evaluates a condition and returns a status of 0 for true, and 1 for false.
test -e /etc/passwd 
then keyword
Opens the body of an if or elif statement.
if true; then echo yes; fi
trap builtin
Catches signals or events to run cleanup code before exit.
trap 'rm -f $TMPFILE' EXIT
uniq command
Filters adjacent duplicate lines. Usually used after sort.
sort | uniq
wc command
Counts lines (-l), words (-w), or characters (-c).
wc -l file.txt
while keyword
Loops as long as a condition command returns 0.
while read -r line; do echo "$line"; done < file
xargs command
Builds and executes commands from stdin, useful after find or grep.
find . -name '*.log' | xargs rm
|| operator
Run the second command only if the first fails (non-zero exit).
cd dir || exit 1
#!/bin/bash shebang
First line โ€” tells OS to use bash interpreter.
#!/bin/bash
set -e option
Exit immediately if any command fails.
set -e
set -u option
Treat unset variables as errors.
set -u
syntax
Expands variable value. Always quote: .
echo 
| operator
Pipe โ€” passes stdout to stdin of next command.
ls | grep go
> operator
Redirect stdout to file, overwriting it.
echo hi > file.txt
>> operator
Redirect stdout, appending to file.
echo hi >> log.txt
&& operator
Run next only if previous succeeded.
make && ./run
if/then/fi syntax
Conditional โ€” runs block if condition true.
if [ -f f ]; then echo ok; fi
for/do/done syntax
Loop over list or glob.
for f in *.txt; do echo ; done
echo command
Prints text to stdout.
echo Hello
grep command
Searches for pattern in files or stdin.
grep -r error logs/
find command
Finds files matching criteria.
find . -name *.log -delete
curl command
Transfers data from/to URLs.
curl -sL https://example.com