System & Server
Updated for 2026

Bash Shell Commands & Scripting Cheatsheet 2026

Quick reference for Bash commands: file operations, piping, process control, environment variables, conditions, and shell scripting loops.

Navigation & Files

ls -la
List all directory contents, including hidden files, with detailed metadata lists.

When to Use

When listing directory contents and you need to review hidden files (starting with dot, like .env or .git) alongside permissions and owners.

Common Mistakes

Thinking the system is frozen because 'ls -la' output is extremely long. Pipette it to a pager if needed.

Shortcut / Pro-Tip

Create shell alias: alias ll='ls -la'

Example

ls -la

Output Example

Console / Terminal
drwxr-xr-x  14 admin  staff    448 Jul 12 09:00 .
drwxr-xr-x+ 78 admin  staff   2496 Jul 12 08:30 ..
-rw-r--r--   1 admin  staff    350 Jul 12 09:12 .env
-rw-r--r--   1 admin  staff   1024 Jul 12 09:00 package.json
mkdir -p /path/to/dir
Create nested directories, automatically building parent paths if missing.

When to Use

When automating UNIX system files, processing pipeline logs, or creating repeatable configuration scripts in local shell windows.

Common Mistakes

Forgetting to make scripts executable using 'chmod +x script.sh', causing Permission Denied execution errors.

Shortcut / Pro-Tip

Add 'set -e' at the top of your scripts to automatically halt execution immediately if any command fails.

Example

mkdir -p /path/to/dir

Output Example

Console / Terminal
Command output printed in terminal standard output (stdout)
rm -rf /path
Force recursively delete a directory or file without prompt confirmations.

When to Use

When automating UNIX system files, processing pipeline logs, or creating repeatable configuration scripts in local shell windows.

Common Mistakes

Forgetting to make scripts executable using 'chmod +x script.sh', causing Permission Denied execution errors.

Shortcut / Pro-Tip

Add 'set -e' at the top of your scripts to automatically halt execution immediately if any command fails.

Example

rm -rf /path

Output Example

Console / Terminal
Command output printed in terminal standard output (stdout)

Pipes & Redirection

grep -i "error" log.txt
Case-insensitive search for a text string inside a file.

When to Use

When locating target characters, filtering log streams, or routing data payloads across multiple terminal filters.

Common Mistakes

Failing to quote search patterns containing special regular expressions, leading to file-matching expansion errors.

Shortcut / Pro-Tip

Use 'grep -E' for extended regular expression features, enabling OR matching patterns (like 'err|fail').

Example

grep -i "error" log.txt

Output Example

Console / Terminal
Command output printed in terminal standard output (stdout)
cat log.txt | grep -E "404|500" > failures.txt
Pipe outputs together and redirect matching results to create a new file.

When to Use

When locating target characters, filtering log streams, or routing data payloads across multiple terminal filters.

Common Mistakes

Failing to quote search patterns containing special regular expressions, leading to file-matching expansion errors.

Shortcut / Pro-Tip

Use 'grep -E' for extended regular expression features, enabling OR matching patterns (like 'err|fail').

Example

cat log.txt | grep -E "404|500" > failures.txt

Output Example

Console / Terminal
Command output printed in terminal standard output (stdout)
tail -f /var/log/nginx/access.log
Follow and print incoming updates to a log file in real-time.

When to Use

When automating UNIX system files, processing pipeline logs, or creating repeatable configuration scripts in local shell windows.

Common Mistakes

Forgetting to make scripts executable using 'chmod +x script.sh', causing Permission Denied execution errors.

Shortcut / Pro-Tip

Add 'set -e' at the top of your scripts to automatically halt execution immediately if any command fails.

Example

tail -f /var/log/nginx/access.log

Output Example

Console / Terminal
Command output printed in terminal standard output (stdout)

Variables & Logic

MY_VAR="value"
Declare a local shell variable (no spaces around equal sign).

When to Use

When automating UNIX system files, processing pipeline logs, or creating repeatable configuration scripts in local shell windows.

Common Mistakes

Forgetting to make scripts executable using 'chmod +x script.sh', causing Permission Denied execution errors.

Shortcut / Pro-Tip

Add 'set -e' at the top of your scripts to automatically halt execution immediately if any command fails.

Example

MY_VAR="value"

Output Example

Console / Terminal
Command output printed in terminal standard output (stdout)
if [ $? -eq 0 ]; then echo "Success"; fi
Check the exit status of the previously executed command (0 indicates success).

When to Use

When automating UNIX system files, processing pipeline logs, or creating repeatable configuration scripts in local shell windows.

Common Mistakes

Forgetting to make scripts executable using 'chmod +x script.sh', causing Permission Denied execution errors.

Shortcut / Pro-Tip

Add 'set -e' at the top of your scripts to automatically halt execution immediately if any command fails.

Example

if [ $? -eq 0 ]; then echo "Success"; fi

Output Example

Console / Terminal
Command output printed in terminal standard output (stdout)

Loops

for file in *.txt; do echo $file; done
Loop through all files matching a wildcard pattern inside the current directory.

When to Use

When automating UNIX system files, processing pipeline logs, or creating repeatable configuration scripts in local shell windows.

Common Mistakes

Forgetting to make scripts executable using 'chmod +x script.sh', causing Permission Denied execution errors.

Shortcut / Pro-Tip

Add 'set -e' at the top of your scripts to automatically halt execution immediately if any command fails.

Example

for file in *.txt; do echo $file; done

Output Example

Console / Terminal
Command output printed in terminal standard output (stdout)
while read line; do echo $line; done < file.txt
Read and print a file line-by-line sequentially.

When to Use

When automating UNIX system files, processing pipeline logs, or creating repeatable configuration scripts in local shell windows.

Common Mistakes

Forgetting to make scripts executable using 'chmod +x script.sh', causing Permission Denied execution errors.

Shortcut / Pro-Tip

Add 'set -e' at the top of your scripts to automatically halt execution immediately if any command fails.

Example

while read line; do echo $line; done < file.txt

Output Example

Console / Terminal
Command output printed in terminal standard output (stdout)

Process Control

ps aux | grep node
List active system processes filtered to show running Node.js engines.

When to Use

When locating target characters, filtering log streams, or routing data payloads across multiple terminal filters.

Common Mistakes

Failing to quote search patterns containing special regular expressions, leading to file-matching expansion errors.

Shortcut / Pro-Tip

Use 'grep -E' for extended regular expression features, enabling OR matching patterns (like 'err|fail').

Example

ps aux | grep node

Output Example

Console / Terminal
Command output printed in terminal standard output (stdout)
kill -9 <PID>
Send a SIGKILL signal to immediately force-terminate a process.

When to Use

When automating UNIX system files, processing pipeline logs, or creating repeatable configuration scripts in local shell windows.

Common Mistakes

Forgetting to make scripts executable using 'chmod +x script.sh', causing Permission Denied execution errors.

Shortcut / Pro-Tip

Add 'set -e' at the top of your scripts to automatically halt execution immediately if any command fails.

Example

kill -9 <PID>

Output Example

Console / Terminal
Command output printed in terminal standard output (stdout)

Bash Best Practices

1Quote All Variable Expansions

Always wrap variables in double quotes (e.g. "$FILE") to prevent word splitting and path globbing errors on spaces.

2Enforce Strict Error Handling (set -e)

Write 'set -euo pipefail' at the top of scripts to crash immediately on command failures, unset variables, or pipe crashes.

3Prefer [[ over [ for Conditional Tests

Use the modern [[ ... ]] operator for comparisons; it is safer, handles empty variables better, and supports regex matching.

4Always Include Shebang Header

Write '#!/usr/bin/env bash' on the very first line of your scripts to ensure they execute under the correct shell environment.

5Use Local Variables in Custom Functions

Declare variables with 'local' inside bash functions to prevent them from leaking into and contaminating the global shell namespace.

Common Bash Errors & Solutions

Error

Permission Denied when running script

Solution

The file does not have execution permission. Grant it using: chmod +x script.sh.

Error

[: missing `]'

Solution

Syntactic space missing. Ensure there are spaces surrounding brackets in comparisons, e.g. use 'if [ "$x" = "y" ]' instead of '[ "$x"'.

Error

CRLF line terminators (DOS format)

Solution

Script edited on Windows contains carriage returns causing syntax errors in Linux. Clean it with: dos2unix script.sh.

Error

Variable contains spaces breaking commands

Solution

Double-quote the variable expansion, for example use 'rm -- "$filename"' instead of 'rm $filename'.

Error

Unbound variable error

Solution

When using 'set -u', accessing a variable that hasn't been set crashes the script. Provide a default value: '${VAR:-default}'.

Common Bash Interview Questions

Q1What does the shebang '#!/usr/bin/env bash' do?

It tells the operating system's program loader to use the first 'bash' binary found in the system's PATH variable to execute the remaining script lines.

Q2What is the difference between $? and $ in Bash?

$? stores the exit status code of the most recently executed foreground command (0 is success, non-zero is failure). $ stores the Process ID (PID) of the current running shell script.

Q3How does double quotes ("") differ from single quotes ('') in shell variable expansion?

Double quotes allow variable expansion ($VAR) and command substitution ($(...)). Single quotes treat every character literally, suppressing all expansions.

Q4What does '2>&1' mean in shell redirection?

It redirects file descriptor 2 (Standard Error) to the same location currently pointed to by file descriptor 1 (Standard Output), capturing both error and normal logs together.

Q5What is the difference between standard and export variables?

A standard variable is only accessible within the current shell process. An exported variable (using 'export VAR') is copied into the environment of any child processes spawned by that shell.