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
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 -laOutput Example
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.jsonWhen 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/dirOutput Example
Command output printed in terminal standard output (stdout)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 /pathOutput Example
Command output printed in terminal standard output (stdout)Pipes & Redirection
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.txtOutput Example
Command output printed in terminal standard output (stdout)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.txtOutput Example
Command output printed in terminal standard output (stdout)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.logOutput Example
Command output printed in terminal standard output (stdout)Variables & Logic
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
Command output printed in terminal standard output (stdout)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"; fiOutput Example
Command output printed in terminal standard output (stdout)Loops
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; doneOutput Example
Command output printed in terminal standard output (stdout)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.txtOutput Example
Command output printed in terminal standard output (stdout)Process Control
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 nodeOutput Example
Command output printed in terminal standard output (stdout)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
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
Permission Denied when running script
The file does not have execution permission. Grant it using: chmod +x script.sh.
[: missing `]'
Syntactic space missing. Ensure there are spaces surrounding brackets in comparisons, e.g. use 'if [ "$x" = "y" ]' instead of '[ "$x"'.
CRLF line terminators (DOS format)
Script edited on Windows contains carriage returns causing syntax errors in Linux. Clean it with: dos2unix script.sh.
Variable contains spaces breaking commands
Double-quote the variable expansion, for example use 'rm -- "$filename"' instead of 'rm $filename'.
Unbound variable error
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.
Generated from LearnHubly Developer Cheatsheets
Access interactive sandbox tests, tools, and developer code bases at https://www.learnhubly.com