Guide to Buying Black Friday VPS for 55% Off
SurferCloud launched its biggest sale of the year on No...




Bash scripting is one of the most valuable skills for Linux users, system administrators, and DevOps engineers. It allows you to automate repetitive tasks, schedule processes, and improve overall system efficiency. Whether you're looking to streamline server maintenance or automate daily tasks, learning how to create Bash scripts can save you significant time and effort.
This guide will introduce you to Bash scripting essentials, provide several examples, and explain how to run Bash scripts on a Linux server. In the end, you'll discover how to deploy and run Bash scripts on a SurferCloud VPS for enhanced performance and flexibility.
A Bash script is a plain text file containing a sequence of commands that are executed sequentially by the Bash shell. Instead of typing commands one by one, you can store them in a script file and run them as a program. Bash scripts are typically used to automate server maintenance, backups, file manipulations, and more.
Bash scripts usually have a .sh file extension (for example, myscript.sh), but this is not strictly necessary. To execute the script, you need to give it the right permissions and run it from the terminal.
nano or vim to create a new file. nano myscript.sh#!/bin/bash#!/bin/bash echo "Hello, World!"chmod +x myscript.sh./myscript.shThe simplest Bash script example:
#!/bin/bash
echo "Hello, World!"
This script prints "Hello, World!" to the terminal.
This script creates a backup of a specific directory.
#!/bin/bash
SOURCE_DIR="/var/www/html"
BACKUP_DIR="/backup/$(date +%Y-%m-%d)"
mkdir -p $BACKUP_DIR
cp -r $SOURCE_DIR/* $BACKUP_DIR
echo "Backup completed. Files saved in $BACKUP_DIR"
How it works:
SOURCE_DIR and BACKUP_DIR are variables that store paths.mkdir -p creates a directory (including any parent directories that do not exist).cp -r copies files recursively from SOURCE_DIR to BACKUP_DIR.This script checks disk space usage and sends an alert if usage exceeds 80%.
#!/bin/bash
THRESHOLD=80
USAGE=$(df / | grep / | awk '{ print $5 }' | sed 's/%//')
if [ $USAGE -gt $THRESHOLD ]; then
echo "Disk space usage is above $THRESHOLD%. Current usage: $USAGE%."
fi
How it works:
df / command shows disk usage, and awk extracts the percentage of disk space used.This script sends an alert when a new user logs in.
#!/bin/bash
who | while read user_info; do
echo "User Login Detected: $user_info"
done
How it works:
who command lists currently logged-in users.while read loop processes each login entry and prints an alert.This script automatically updates system packages on Ubuntu.
#!/bin/bash
echo "Updating system packages..."
sudo apt-get update -y
sudo apt-get upgrade -y
echo "System update completed."
How it works:
sudo apt-get update refreshes the package list.sudo apt-get upgrade installs available package updates.-y option automatically answers "yes" to any prompts.NAME="SurferCloud" echo "Welcome to $NAME"if [ $1 -gt 10 ]; then echo "The argument is greater than 10" else echo "The argument is less than or equal to 10" fifor i in 1 2 3 4 5; do echo "Loop number $i" donegreet_user() { echo "Welcome, $1!" } greet_user "John"These structures are essential for creating dynamic and robust Bash scripts.
When it comes to running Bash scripts on a VPS, SurferCloud makes it easy. Here's how you can run a Bash script on a SurferCloud VPS:
ssh user@your-vps-ipnano. nano script.sh#!/bin/bash echo "Running maintenance tasks"chmod +x script.sh./script.shcrontab -e Add the following line to run the script every day at 2 a.m.: 0 2 * * * /path/to/script.shIf you want to run Bash scripts on a secure, high-performance VPS, SurferCloud is the perfect option. Here’s why:
With SurferCloud, you get the reliability and performance required for running powerful Bash scripts at scale.
Get started with SurferCloud VPS and deploy your first Bash script in just minutes!
Bash scripting is an essential skill for anyone managing Linux servers. It simplifies repetitive tasks, automates maintenance, and saves valuable time. With this guide, you’ve learned how to create, run, and automate Bash scripts for various use cases like backups, updates, and user monitoring.
If you want a fast, secure, and global platform to run your Bash scripts, SurferCloud VPS is the way to go. It offers low-latency servers, robust security, and flexible configurations for developers, DevOps engineers, and system administrators.
SurferCloud launched its biggest sale of the year on No...
DeepSeek-R1 is an open-source reasoning model designed ...
Looking for a reliable and affordable VPS hosting solut...