Skip to content
Log In Sign Up info@muvcloud.com

Your First Script

Create a file named hello.sh:

#!/usr/bin/env bash
echo "Hello from $(hostname)"
echo "Today is $(date +%F)"

The first line is the shebang. It tells the system which program runs the file. #!/usr/bin/env bash finds Bash wherever it is installed; #!/bin/bash also works on every common Linux distribution.

Make It Executable and Run It

chmod +x hello.sh
./hello.sh

The ./ is needed because the current folder is not in your PATH. You can also run a script without the execute bit with bash hello.sh. To run it by name from anywhere, put it in /usr/local/bin, or in ~/bin, which the default startup files add to PATH (on Debian and Ubuntu, once the folder exists and you log in again); how PATH works is explained in how to set environment variables in Linux.

Variables

#!/usr/bin/env bash
site="example.com"
backup_dir="/var/backups/$site"
today="$(date +%F)"
echo "Backing up $site to $backup_dir on $today"
  • No spaces around =: site = "x" is read as a command named site.
  • Quote every use: write "$backup_dir", so a value with spaces stays one word.
  • Command output: $(command) puts a command's output into a variable.

Arguments

Values given after the script name arrive as numbered variables:

#!/usr/bin/env bash
echo "Script name: $0"
echo "First argument: $1"
echo "Number of arguments: $#"
for arg in "$@"; do
    echo "Argument: $arg"
done

Run it as ./args.sh one "two words". "$@" expands to every argument, each kept as one word, so "two words" stays together. Use it rather than $*.

Exit Codes

Every command returns a number: 0 for success, anything else for failure. $? holds the code of the last command, and exit sets your script's own code:

#!/usr/bin/env bash
if [[ $# -lt 1 ]]; then
    echo "Usage: $0 DIRECTORY" >&2
    exit 1
fi
if ! tar -czf "/var/backups/site-$(date +%F).tar.gz" "$1"; then
    echo "Backup failed" >&2
    exit 1
fi
echo "Backup done"

>&2 sends the message to standard error, where error messages belong. systemd marks a service whose script exits with a non-zero code as failed, and monitoring tools read the code the same way. The [[ … ]] test is covered in the Bash if statement guide; in short, it is Bash's safer form of the older [ … ] command.

Stop on Errors with set -euo pipefail

By default a script keeps going after a failed command. Many scripts start with:

#!/usr/bin/env bash
set -euo pipefail
  • -e: exit when a command fails. Commands tested by if or while, commands negated with !, and commands before the last && or || in a list do not trigger it.
  • -u: treat an unset variable as an error, which catches typos in variable names.
  • -o pipefail: a pipeline fails if any command in it fails, not only the last.

These options do not catch every mistake; still check the results of important steps yourself. With -u, give optional arguments a default: name="${1:-default}".

Check a Script Before Running It

bash -n backup.sh
bash -x backup.sh /var/www/example.com

-n checks the syntax without running anything. -x prints each command as it runs, which shows where a script goes wrong. ShellCheck finds quoting mistakes and other common bugs; install it with sudo apt install shellcheck on Ubuntu and Debian, or enable the EPEL repository on AlmaLinux and Rocky Linux and run sudo dnf install ShellCheck.

Next Steps

Repeat work with Bash for loops and make decisions with if statements. If a script writes files to a server folder, check the space it has first with df and du.

Updated:

Linux VPS with Full Root Access

MUVCloud cloud servers run on KVM with Enterprise NVMe SSD storage and full root access, in Turkey, the Netherlands, and the USA.

Cloud VPS Plans Starting at2.99 $/mo Cheap Linux VPS

No Limits on Service or Support

You are not on your own as your projects grow. Our support engineers are ready 24/7, on several channels, whenever you need them.

Support specialist
  • 100% customer satisfaction goal
  • Our own hardware
  • 24/7 ticket and email support
  • 4 countries with locations