UnderHost
Knowledgebase Docs

Bash Scripting Basics: Write Your First Script

Learn bash scripting fundamentals: variables, loops, conditionals, functions, and error handling. Write automated scripts for Linux/Unix server management.

On this page

Bash is the default shell on Linux and macOS. Bash scripts automate repetitive tasks: backups, system updates, log monitoring, database maintenance. Learning bash scripting is essential for VPS/Dedicated server management and system administration.

Your First Script

Create a file named hello.sh:

#!/bin/bash
echo "Hello, World!"

The #!/bin/bash line tells the system to use bash to execute this script. Make it executable:

chmod +x hello.sh
./hello.sh

Variables and Data

Store and use variables:

#!/bin/bash
name="John"
age=25
echo "Hello, $name! You are $age years old."

# Get command output
today=$(date +%Y-%m-%d)
echo "Today is $today"

Conditionals (if/else)

Make decisions in scripts:

#!/bin/bash
age=25

if [ $age -ge 18 ]; then
  echo "You are an adult"
else
  echo "You are a minor"
fi

# Check if file exists
if [ -f "/etc/hostname" ]; then
  echo "File exists"
else
  echo "File not found"
fi

Common test operators: -eq (equal), -ne (not equal), -gt (greater), -lt (less), -f (file exists), -d (directory exists)

Loops (for/while)

For loop - iterate over items:

#!/bin/bash
for i in 1 2 3 4 5; do
  echo "Number: $i"
done

# Loop through files
for file in *.txt; do
  echo "Found: $file"
done

While loop - repeat while condition is true:

#!/bin/bash
counter=1
while [ $counter -le 5 ]; do
  echo "Count: $counter"
  counter=$((counter + 1))
done

Functions

Reusable blocks of code:

#!/bin/bash
greet() {
  echo "Hello, $1!"
}

# Call the function
greet "Alice"
greet "Bob"

# Function with return value
add() {
  return $(($1 + $2))
}

add 5 3
result=$?
echo "Sum is: $result"

Error Handling

Check if commands succeed or fail:

#!/bin/bash
# $? contains the exit code of the last command
if [ $? -eq 0 ]; then
  echo "Success!"
else
  echo "Command failed"
fi

# Exit on error (stop script immediately)
set -e
apt update || exit 1
apt install nginx -y || exit 1

Best Practices

  • Always use the shebang: #!/bin/bash at the top
  • Use meaningful variable names: backup_path not bp
  • Add comments: Explain what complex sections do
  • Quote variables: Use "$variable" to prevent issues with spaces
  • Check for errors: Use set -e or check $? after critical commands
  • Test scripts locally first: Run with bash -x script.sh to debug
  • Make scripts readable: Use functions to avoid repetition
Schedule scripts with cron

Run bash scripts on a schedule using cron. Edit crontab -e and add: 0 2 * * * /path/to/script.sh (runs daily at 2 AM). See our Cron jobs guide for more.

Related: Command line basics | Cron jobs and scheduling | Automation scripts

Was this article helpful?

Need a developer-friendly server?

Use an UnderHost Cloud VPS for SSH, Git, Node.js, Python, Laravel, Docker, cron, and custom development workflows.

Back to Developer Tools