Skip to content

6.1 Bash Scripting Basics

A script is just a text file with a list of commands.

The first line tells Linux which interpreter to use.

#!/bin/bash

Create hello.sh:

#!/bin/bash

# Define a variable (No spaces in assignment!)
NAME="World"

# Print it using $
echo "Hello, $NAME"

Make it executable: chmod +x hello.sh. Run it: ./hello.sh.

if [ -f "/etc/passwd" ]; then
    echo "Filesystem is sane."
else
    echo "Something is very wrong."
fi
for i in {1..5}; do
    echo "Count: $i"
done

Every command returns a status.

  • 0: Success.
  • Non-zero: Error. You check it with $?.