Bash Cheat Sheet

Comprehensive Bash commands, shell syntax, quoting, variables, redirection, pipelines, and day-to-day scripting patterns.

View
StandardDetailedCompact
Export
Copy the compact sheet, download it, or print it.
Download
`D` dense toggle · `C` copy all

Invocation and Help

Start Bash, inspect version, and open a clean shell.

Show Bash version

Print installed Bash version.

bashANYversionhelp
bash
bash --version

Useful when checking feature support such as associative arrays or newer shell options.

Start login shell

Start Bash as a login shell.

bashANYloginstartup
bash
bash -l

Loads login startup files such as /etc/profile and ~/.bash_profile depending on system configuration.

Start clean interactive shell

Start Bash without profile or rc files.

bashANYdebugstartup
bash
bash --noprofile --norc

Useful for debugging startup configuration problems.

Run one command and exit

Run a command string in a login shell.

bashANYinvocationcommand
bash
bash -lc 'echo "$SHELL" && pwd'

Common in CI, Docker, and automation where you need Bash semantics for one command.

Check script syntax

Parse a script without executing it.

bashANYsyntaxlint
bash
bash -n script.sh

Good first step before running a script in production or CI.

Run with execution trace

Print commands as they execute.

bashANYtracedebug
bash
bash -x script.sh

Helps debug variable expansion and flow control problems.

Shell Basics

Basic commands, chaining, and status codes.

Print previous exit status

Show exit status of previous command.

bashANYexit-statusbasics
bash
echo $?

A zero status means success by convention.

Run command sequence

Run later commands only if earlier ones succeed.

bashANYoperatorssequence
bash
mkdir -p tmp && cd tmp && pwd

Use && for fail-fast sequences.

Run fallback command

Run fallback command when previous command fails.

bashANYoperatorsfallback
bash
grep -q needle file.txt || echo 'not found'

Use || for error handling or defaults.

Group commands in current shell

Run a grouped list in current shell.

bashANYgroupingshell
bash
{ echo start; date; echo end; }

Braces keep variable changes in current shell when syntax is valid.

Run commands in subshell

Run a grouped list in a subshell.

bashANYsubshellgrouping
bash
( cd /tmp && pwd )

Directory changes and variables do not leak back to parent shell.

Pipeline commands

Send output from one command to another.

bashANYpipesfilters
bash
ps aux | grep nginx | grep -v grep

Pipelines are foundational to shell workflows.

Enable pipefail

Make pipeline fail if any command fails.

bashANYpipefailset
bash
set -o pipefail

Important for reliable scripts; otherwise only final pipeline status is returned.

Redirection

Redirect stdin, stdout, stderr, and combine streams.

Redirect stdout to file

Overwrite a file with command output.

bashANYstdoutredirect
bash
echo 'hello' > out.txt

Use >> to append instead of overwrite.

Append stdout to file

Append output to a file.

bashANYstdoutappend
bash
echo 'hello' >> out.txt

Useful for logs and generated reports.

Redirect stderr to file

Write error output to a separate file.

bashANYstderrredirect
bash
grep needle missing.txt 2> errors.log

File descriptor 2 is stderr.

Redirect stdout and stderr

Send both stdout and stderr to same file.

bashANYstderrstdoutfd
bash
command > all.log 2>&1

Very common for logs and debugging shell jobs.

Discard command output

Silence both stdout and stderr.

bashANYsilencedevnull
bash
command > /dev/null 2>&1

Useful in scripts when output is expected but unneeded.

Use a here document

Feed literal multiline input to a command.

bashANYheredocinput
bash
cat <<'EOF'
line 1
$HOME is not expanded here
EOF

Quoted delimiter disables parameter expansion.

Use a here string

Pass a short string to stdin.

bashANYherestringstdin
bash
grep -o '[0-9]\+' <<< 'order-123'

Useful for compact one-liners and tests.

Read file line by line

Read a file safely without trimming backslashes.

bashANYreadfiles
bash
while IFS= read -r line; do echo "$line"; done < input.txt

The IFS= and -r pattern is the standard safe read loop.