AI Generate Bash docs instantly

Bash Cheat Sheet

Quick reference guide with copy-paste ready code snippets

Try DocuWriter Free

Variables

6 snippets

Assign

name="John"

Use

echo $name
echo ${name}

Read Input

read -p "Enter name: " name

Array

fruits=("apple" "banana" "cherry")

Array Access

echo ${fruits[0]}
echo ${fruits[@]}

Length

echo ${#name}  # String length
echo ${#fruits[@]}  # Array length

Control Flow

5 snippets

If/Else

if [ $x -gt 0 ]; then
  echo "positive"
elif [ $x -lt 0 ]; then
  echo "negative"
else
  echo "zero"
fi

For Loop

for i in 1 2 3 4 5; do
  echo $i
done

For Range

for i in {1..5}; do
  echo $i
done

While Loop

while [ $count -lt 10 ]; do
  ((count++))
done

Case

case $day in
  "Mon") echo "Monday" ;;
  "Tue") echo "Tuesday" ;;
  *) echo "Other" ;;
esac

Comparisons

3 snippets

Numbers

-eq  # Equal
-ne  # Not equal
-gt  # Greater than
-lt  # Less than
-ge  # Greater or equal
-le  # Less or equal

Strings

=   # Equal
!=  # Not equal
-z  # Empty
-n  # Not empty

Files

-f  # Is file
-d  # Is directory
-e  # Exists
-r  # Readable
-w  # Writable
-x  # Executable

Tired of looking up syntax?

DocuWriter.ai generates documentation and explains code using AI.

Try Free

Functions

4 snippets

Function

greet() {
  echo "Hello, $1!"
}

Call

greet "World"

Return Value

add() {
  echo $(($1 + $2))
}
result=$(add 5 3)

Local Variable

myfunc() {
  local name="John"
}

String Operations

5 snippets

Concatenate

full="$first $last"

Substring

echo ${name:0:5}  # First 5 chars

Replace

echo ${name/old/new}  # First occurrence
echo ${name//old/new}  # All occurrences

Uppercase

echo ${name^^}

Lowercase

echo ${name,,}

File Operations

4 snippets

Read File

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

Write File

echo "text" > file.txt   # Overwrite
echo "text" >> file.txt  # Append

Check File

if [ -f "file.txt" ]; then
  echo "File exists"
fi

Loop Files

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

Common Commands

4 snippets

Pipe

cat file.txt | grep "pattern"

Redirect

command > output.txt 2>&1

Command Sub

today=$(date +%Y-%m-%d)

Exit Status

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

More Cheat Sheets

FAQ

Frequently asked questions

What is a Bash cheat sheet?

A Bash cheat sheet is a quick reference guide containing the most commonly used syntax, functions, and patterns in Bash. It helps developers quickly look up syntax without searching through documentation.

How do I learn Bash quickly?

Start with the basics: variables, control flow, and functions. Use this cheat sheet as a reference while practicing. For faster learning, try DocuWriter.ai to automatically explain code and generate documentation as you learn.

What are the most important Bash concepts?

Key Bash concepts include variables and data types, control flow (if/else, loops), functions, error handling, and working with data structures like arrays and objects/dictionaries.

How can I document my Bash code?

Use inline comments for complex logic, docstrings for functions and classes, and README files for projects. DocuWriter.ai can automatically generate professional documentation from your Bash code using AI.

Code Conversion Tools

Convert Bash to Other Languages

Easily translate your Bash code to other programming languages with our AI-powered converters

Related resources

Stop memorizing. Start shipping.

Generate Bash Docs with AI

DocuWriter.ai automatically generates comments, docstrings, and README files for your code.

Auto-generate comments
Create README files
Explain complex code
API documentation
Start Free - No Credit Card

Join 33,700+ developers saving hours every week