Bash Scripting Language Cheatsheet

Bash, short for “Bourne Again SHell,” is a powerful and widely used scripting language on Unix and Unix-like systems. It serves as a command processor, allowing users to interact with the operating system and automate tasks through scripts. Whether you’re a beginner or an experienced developer, having a cheatsheet handy can be incredibly useful for quick reference. In this blog post, we’ll provide a comprehensive Bash programming language cheatsheet to help you navigate the essentials.

1. Variables

# Declare a variable
variable_name="Hello, World!"

# Access variable value
echo $variable_name

# Concatenate strings
greeting="Hello"
name="John"
message="$greeting, $name!"
echo $message

2. Conditionals

# If statement
if [ condition ]; then
    # code to execute if the condition is true
fi

# If-else statement
if [ condition ]; then
    # code to execute if the condition is true
else
    # code to execute if the condition is false
fi

# Example
if [ $num -gt 10 ]; then
    echo "Greater than 10"
else
    echo "Less than or equal to 10"
fi

3. Loops

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

# While loop
counter=0
while [ $counter -lt 5 ]; do
    echo $counter
    ((counter++))
done

# Until loop
counter=0
until [ $counter -ge 5 ]; do
    echo $counter
    ((counter++))
done

4. Functions

# Declare a function
function_name() {
    # code for the function
}

# Call a function
function_name

# Function with parameters
greet() {
    echo "Hello, $1!"
}

# Call function with parameter
greet "John"

5. Input/Output

# Read input
echo "Enter your name:"
read name
echo "Hello, $name!"

# Command substitution
current_date=$(date)
echo "Current date: $current_date"

# Redirect output to a file
echo "Hello, World!" > output.txt

6. File Operations

# Check if a file exists
if [ -e file.txt ]; then
    echo "File exists"
fi

# Check if a directory exists
if [ -d directory ]; then
    echo "Directory exists"
fi

# Copy a file
cp source.txt destination.txt

# Move/Rename a file
mv oldname.txt newname.txt

# Remove/Delete a file
rm file.txt

7. Special Variables

# Positional parameters
echo "Script name: $0"
echo "First argument: $1"
echo "Second argument: $2"
# and so on...

# Number of arguments
echo "Number of arguments: $#"

# Last command exit status
echo "Last command exit status: $?"

8. Miscellaneous

# Sleep for n seconds
sleep 5

# Case statement
case $variable in
    pattern1)
        # code for pattern1
        ;;
    pattern2)
        # code for pattern2
        ;;
    *)
        # default code
        ;;
esac

This cheatsheet covers the fundamental aspects of Bash programming. Keep it handy, and it will serve as a quick reference guide to help you write efficient and effective Bash scripts. Happy coding!

Refer to some more documents for in-dept knowledge on Bash Scripting.

1. What is Bash, and why is it widely used?

Bash, short for “Bourne Again SHell,” is a command processor that typically runs in a text window where the user types commands that cause actions. It is widely used on Unix and Unix-like systems due to its powerful scripting capabilities, ease of use, and its role as the default shell on many operating systems, including Linux.

2. How do I declare and use variables in Bash?

To declare a variable in Bash, use the syntax variable_name=value. To access the value, use echo $variable_name.

3. What are the differences between single and double square brackets in Bash conditionals?

In Bash, both single [ ] and double [[ ]] square brackets are used for conditionals. However, [[ ]] provides additional features such as pattern matching and improved handling of variables. Double brackets are generally preferred for more complex conditions, while single brackets are suitable for basic tests.

4. How can I redirect the output of a Bash script to a file?

To redirect the output of a Bash script to a file, you can use the > operator. For example:
echo "Hello, World!" > output.txt
This command will overwrite the contents of output.txt with the output of the echo command. If you want to append to the file, use

5. What is the purpose of the special variable $? in Bash?

The $? special variable in Bash stores the exit status of the last executed command. A value of 0 typically indicates success, while a non-zero value indicates an error. This variable is useful for checking the success of a command within a script and making decisions based on whether the previous command succeeded or failed.