Garden of KnowledgeApplied Sciences › Computer Science › Software › Languages › Bash
February 16, 2026

Bash Basics Cheat Sheet

Bash, or the Bourne Again SHell, is a Unix shell and command language that is widely used for scripting and automation on Linux and macOS systems. Below is a quick reference to help you get started with basic Bash commands and scripting.

1. Basics of Bash Commands§

Syntax§

Basic Commands§

CommandDescription
lsList directory contents
cdChange directory
pwdPrint current directory
mkdirCreate a new directory
rmRemove files or directories
cpCopy files or directories
mvMove or rename files or directories
echoDisplay text or variables
catConcatenate and display file contents
touchCreate a new empty file

File Permissions§

2. Variables and Environment§

Defining Variables§

Environment Variables§

Special Variables§

VariableDescription
$0Script name
$1 to $9Positional arguments (1 to 9)
$#Number of arguments
$@All arguments as a list
$?Exit status of last command
$$Process ID of current shell

3. Basic Control Structures§

Conditional Statements§

if [ condition ]; then
  # commands
elif [ condition ]; then
  # commands
else
  # commands
fi

Cases§

read -p "Enter a choice (start, stop, restart): " action

case "$action" in
  start)
    echo "Starting service..."
    # Commands to start the service
    ;;
  stop)
    echo "Stopping service..."
    # Commands to stop the service
    ;;
  restart)
    echo "Restarting service..."
    # Commands to restart the service
    ;;
  *)
    echo "Invalid option"
    ;;
esac

Loops§

For Loop§

for var in list; do
  # commands
done

While Loop§

while [ condition ]; do
  # commands
done

4. Functions§

Functions in Bash allow for reusable pieces of code.

Defining a Function§

function_name() {
  # commands
}

Calling a Function§

function_name

5. Basic I/O Redirection§

Redirect Output§

Piping§

6. Basic String Operations§

String Comparison§

if [ "$str1" = "$str2" ]; then
  echo "Strings are equal."
fi

String Length§

length=${#str}

Substring Extraction§

substr=${str:position:length}

7. Common Expressions§

Arithmetic Operations§

Use (( )) for arithmetic operations.

result=$((5 + 3))
echo $result  # Output: 8

Logical Operators§

OperatorDescription
-eqEqual
-neNot equal
-gtGreater than
-ltLess than
-geGreater/equal
-leLess/equal

File Operations§

TestDescription
-e filenameFile exists
-f filenameRegular file
-d dirnameDirectory exists
-r filenameFile is readable
-w filenameFile is writable
-x filenameFile is executable

8. Writing Simple Scripts§

To create and run a Bash script:

  1. Create a new file:

    nano script.sh
  2. Add the shebang to indicate the script uses Bash:

    #!/bin/bash
  3. Write your script and save.

  4. Make it executable:

    chmod +x script.sh
  5. Run the script:

    ./script.sh

9. Sample Script§

Here’s a simple script that greets the user with the current date:

#!/bin/bash

# Greet the user
echo "Hello, $(whoami)! Today is $(date +"%A, %B %d, %Y")."
—The Gardener