Log inSign up

Your first Bash script, from empty file to a command on your PATH

Put #!/usr/bin/env bash on the first line of a plain text file, add your commands below it, then run chmod +x script.sh and start it with ./script.sh. Move the finished file into ~/.local/bin or /usr/local/bin and it runs by name from any folder. Check every new script with bash -n and shellcheck before it touches real data.

Topic: Bash scripting · Tested on: Ubuntu 24.04 and 22.04, Debian 12 and 13, Rocky Linux 9, AlmaLinux 9 · Updated 2026-12-03

What exactly is a Bash script?

A Bash script is a text file holding the same commands you would type at the prompt, read and executed line by line by the bash program. Nothing gets compiled. When you save five commands you repeat every week into a file, you have a script, and from then on one word replaces the whole routine.

Every server image you can deploy from the console (Ubuntu 24.04 and 22.04, Debian 12 and 13, Rocky Linux 9, AlmaLinux 9) ships Bash as the login shell for normal users, so there is nothing to install. Log in over SSH as your everyday user (the first SSH session tutorial covers this) and confirm the version:

bash --version | head -n 1
echo "$SHELL"

Ubuntu 24.04 and Debian 12 or 13 report Bash 5.2, while Ubuntu 22.04, Rocky Linux 9 and AlmaLinux 9 report 5.1. Everything in this guide works on both.

How do I create the file and the shebang line?

Open a new file in any editor and make the very first line a shebang: the characters #! followed by the path of the interpreter that should read the rest. Use nano if you have no favourite editor yet:

mkdir -p ~/scripts
cd ~/scripts
nano hello.sh

Type this content and save it with Ctrl+O, then leave with Ctrl+X:

#!/usr/bin/env bash
# hello.sh: print a greeting and a few facts about this server
echo "Hello from $(hostname)"
echo "Today is $(date +%F), logged in as $USER"
echo "Uptime: $(uptime -p)"

The form #!/usr/bin/env bash asks env to find bash on the PATH, which works on every Linux system and on FreeBSD, where Bash lives in /usr/local/bin instead of /bin. On a Linux only setup #!/bin/bash is equally fine. Lines that begin with # after the first one are comments and are ignored. The $(...) parts are command substitutions: Bash runs the inner command and pastes its output into the line.

How do I make the script executable and run it?

Give the file the execute bit with chmod +x, then call it with a path. The ./ prefix matters because the current folder is not on the PATH for safety reasons:

chmod +x hello.sh
ls -l hello.sh
./hello.sh

The ls -l output now shows -rwxr-xr-x or similar; the x letters are the execute permission. If you only want to test something quickly you can skip chmod and run bash hello.sh, which reads the file without needing the execute bit and ignores the shebang. Avoid sh hello.sh: on Ubuntu and Debian sh is Dash, a smaller shell that does not understand Bash features such as arrays or [[ ]], so a working script can suddenly fail. The file permissions guide explains the permission bits in detail.

How do I run a script by name from any directory?

Place it in a folder listed in your PATH variable. For a script only you use, ~/.local/bin is the tidy choice; Ubuntu and Debian add it to the PATH automatically at login once the folder exists, and Rocky and Alma do the same through their default ~/.bashrc:

mkdir -p ~/.local/bin
cp ~/scripts/hello.sh ~/.local/bin/hello
echo "$PATH" | tr ":" "\n"
hash -r
hello

If ~/.local/bin does not appear in the list, log out and back in, or add export PATH="$HOME/.local/bin:$PATH" to ~/.bashrc. Dropping the .sh ending is a common convention for commands, since the caller should not care which language sits inside. For a tool every account on the server should have, install it system wide:

sudo install -m 755 ~/scripts/hello.sh /usr/local/bin/hello
command -v hello

install copies the file and sets owner and mode in one step. Never put your own scripts into /usr/bin, which belongs to the package manager.

How do arguments and exit codes work?

Arguments typed after the script name arrive as $1, $2 and so on, $# holds how many there are, and "$@" expands to all of them with spacing kept intact. The exit code, a number from 0 to 255, tells the caller whether things went well: 0 means success and anything else is an error. Here is a small script that uses both:

#!/usr/bin/env bash
# greet: say hello to each name given
if [ "$#" -eq 0 ]; then
  echo "usage: greet NAME..." >&2
  exit 2
fi
for person in "$@"; do
  echo "Hello, $person"
done
exit 0
./greet Ana "Jo Lee"
./greet; echo "exit code: $?"

The second run prints the usage line to standard error (>&2) and the exit code 2. Always wrap $1 and "$@" in double quotes so that a name containing a space stays one argument. Variables are covered fully in Bash variables and strings.

How can I catch mistakes before running the script?

Run a syntax check with bash -n, then a lint pass with ShellCheck, which spots quoting bugs and risky patterns that Bash itself accepts silently:

bash -n hello.sh && echo "syntax fine"
# Ubuntu and Debian
sudo apt update && sudo apt install -y shellcheck
# Rocky Linux 9 and AlmaLinux 9 (package comes from EPEL)
sudo dnf install -y epel-release
sudo dnf install -y ShellCheck
shellcheck --version
shellcheck hello.sh

Note the capital letters in the package name on the RHEL family; the command itself is lowercase everywhere. Each ShellCheck warning carries a code such as SC2086, and searching that code explains the problem and the fix. For a live trace while the script runs, start it with bash -x: every command is printed with a + in front after variables have been expanded, which makes wrong values obvious.

Where should scripts live on a server?

Keep the working copy in a folder you control, ideally inside a Git repository, and install only the finished version into a PATH folder. A simple layout that scales well:

  • ~/scripts or a Git checkout: drafts and history.
  • ~/.local/bin: personal commands for your user.
  • /usr/local/bin: tools for every account, owned by root, mode 755.
  • /usr/local/sbin: admin tools that only make sense with sudo.

Scripts started by cron or a systemd timer should use absolute paths for files they read and write, because the working directory and PATH are minimal in those contexts. The guide on cron jobs and systemd timers shows how to schedule them. Before you rebuild a server with a fresh image, copy your scripts somewhere safe, since a reinstall erases the disk.

Why does my script say permission denied or command not found?

Permission denied almost always means the execute bit is missing, or the file sits on a filesystem mounted with noexec (common for /tmp on hardened systems). Run chmod +x again or move the file. bad interpreter: No such file or directory usually points to Windows line endings: the shebang then ends with an invisible carriage return. Check and repair it:

file hello.sh
sed -i "s/\r$//" hello.sh

command not found for a script name means the folder is not on the PATH, or Bash cached an old lookup; hash -r clears that cache. A script that runs from the prompt but fails under cron is usually missing a PATH entry or relies on a relative path.

Plans that fit this guide

Base city prices. Every plan includes a dedicated IPv4 address, IPv6, full root and all 18 images.

All prices
PlanFits becauseSizeMonthlyOrder from
A1-1plenty for learning Bash and small admin scripts1 vCPU, 1 GB RAM, 25 GB NVMe$3.60/mo$43.20 for 12 months

Questions about Write and run a Bash script

Does the file need the .sh extension?

No. Linux decides how to run a file from the shebang line and the execute bit, not from its name. The ending is a hint for people and editors, and many installed commands drop it.

Is #!/bin/sh the same as #!/bin/bash?

Not on Ubuntu and Debian, where /bin/sh is Dash. Use sh only for scripts written strictly for POSIX shell; anything using arrays, [[ ]] or $'...' strings needs Bash in the shebang.

How do I stop a script that hangs?

Press Ctrl+C in the terminal to send SIGINT. For a script running in the background, find its process ID with pgrep -af scriptname and end it with kill PID.

Can I run a script after I log out?

Yes. Start it inside a tmux session, use nohup ./job.sh &, or for anything long running turn it into a systemd service.

Which server size do I need just for scripting?

Any plan works, since Bash itself uses a few megabytes. Pick the size for whatever the scripts manage, such as a web stack or a database, not for the scripts.