Bash scripting for beginners part 1 banner
Bash beginner series · Part 1

Bash Scripting for Beginners: Your First Script

This first part gets you from typing commands manually to writing and running a simple Bash script. You will learn the shebang, file permissions, comments, echo, variables, quoting and a practical server report script.

On this page
Basics

What is a Bash script?

A Bash script is a plain text file containing shell commands. Instead of typing the same commands repeatedly, you save them in a file and run them as a small program.

Good for

Health checks, backups, log searches, service checks, file cleanup and repeatable admin tasks.

Not magic

A script runs the commands you give it. If a command is dangerous manually, it is still dangerous in a script.

Beginner rule

Start by printing what the script will do before making changes. Your future self will approve.

Step 1

Create your first Bash script

Create a file named server-report.sh.

nano server-report.sh

Add this:

#!/usr/bin/env bash

# Simple server report script
echo "Server report"
echo "-------------"
echo "Hostname: $(hostname)"
echo "Date: $(date)"
echo "Current user: $(whoami)"
echo "Current directory: $(pwd)"

The first line is the shebang. It tells the system to run this script with Bash.

Step 2

Make the script executable and run it

Before running with ./server-report.sh, make the file executable.

chmod +x server-report.sh
./server-report.sh
$ ./server-report.sh
Server report
-------------
Hostname: web01
Date: Sun May  3 14:22:41 BST 2026
Current user: exampleuser
Current directory: /home/exampleuser
If you get Permission denied, you probably forgot chmod +x server-report.sh.
Important

Bash vs sh

These guides use Bash, not plain POSIX sh. That matters because later examples use Bash features such as arrays.

CommandWhat it means
./script.shRuns the script using the interpreter from the shebang, such as #!/usr/bin/env bash.
bash script.shRuns the script with Bash explicitly.
sh script.shMay run with a more limited shell. Avoid this for Bash-specific scripts.
Beginner rule: use #!/usr/bin/env bash, then run the script with ./script.sh after making it executable.
Variables

Variables and quoting

Variables store values. In Bash, do not put spaces around the equals sign.

SERVER_NAME="web01"
echo "$SERVER_NAME"

Quote variables when using them. This prevents problems with spaces, empty values and wildcard expansion.

GoodRisky
echo "$FILE"echo $FILE
rm "$BACKUP_FILE"rm $BACKUP_FILE
Line by line

What the first script is doing

LineMeaning
#!/usr/bin/env bashUse Bash to run the script.
# Simple server report scriptA comment for humans. Bash ignores it.
echo "Server report"Prints text to the terminal.
$(hostname)Runs the hostname command and inserts the result.
$(pwd)Prints the current working directory.
Example script

Practical script: basic Linux health report

This script prints useful starter information about a Linux server.

#!/usr/bin/env bash

HOST="$(hostname)"
TODAY="$(date -Is)"
UPTIME="$(uptime -p)"

echo "Linux health report"
echo "Host: $HOST"
echo "Generated: $TODAY"
echo "Uptime: $UPTIME"
echo

echo "Disk usage:"
df -h /

echo
echo "Memory usage:"
free -h

echo
echo "Failed systemd units:"
systemctl --failed --no-pager
Output

Example output

$ ./health-report.sh
Linux health report
Host: web01
Generated: 2026-05-03T14:30:18+01:00
Uptime: up 12 days, 4 hours, 21 minutes

Disk usage:
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda2        80G   42G   35G  55% /

Memory usage:
               total        used        free      shared  buff/cache   available
Mem:           7.7Gi       2.4Gi       1.1Gi       180Mi       4.2Gi       4.8Gi

Failed systemd units:
  UNIT LOAD ACTIVE SUB DESCRIPTION
0 loaded units listed.
Try it yourself

Beginner exercise

Create a script called my-first-report.sh that prints:

  • the hostname
  • today's date
  • the current user
  • disk usage for /

Expected style of output:

$ ./my-first-report.sh
My first report
Host: web01
User: exampleuser
Disk usage:
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda2        80G   42G   35G  55% /
Downloads

Download the example scripts

You can download the Part 1 example scripts and test them on a safe Linux machine or VM.

Troubleshooting

Common errors and fixes

ErrorLikely causeFix
Permission deniedThe script is not executable.chmod +x script.sh
bad interpreterWrong shebang or Windows line endings.Check the first line, or run dos2unix script.sh.
command not foundTypo, missing command, or running from the wrong path.Check spelling and use ./script.sh.

You can also check basic syntax with:

bash -n script.sh
Mistakes

Common beginner mistakes

  • Using NAME = value instead of NAME="value".
  • Forgetting to make the script executable.
  • Running the script from the wrong directory.
  • Forgetting quotes around variables.
  • Trying to automate destructive commands before testing safely.
Next

Next: arguments, if statements and exit codes

Part 2 teaches scripts that accept input, make decisions and return useful success or failure codes.

Beginner checklist

Your first Bash script checklist

StepCommand or action
Create the filenano hello.sh
Add a shebang#!/usr/bin/env bash
Make it executablechmod +x hello.sh
Run it./hello.sh
Debug if neededbash -x hello.sh
Output example

Example first script with output

#!/usr/bin/env bash

name="Linux learner"
echo "Hello, $name"
echo "Today is $(date +%F)"
Example output:
Hello, Linux learner
Today is 2026-05-05

Next, continue with Part 2, then Part 3.

FAQ

Frequently Asked Questions

What does the Bash shebang do?

The shebang tells the system which interpreter should run the script, such as /usr/bin/env bash.

Why do I need chmod +x for a script?

chmod +x makes the script executable so it can be run directly as ./script.sh.

How do I debug a beginner Bash script?

Run bash -x script.sh to print each command as Bash executes it.

Should beginners use .sh file extensions?

It is common and useful for learning, but executable scripts do not technically require a .sh extension.

$ practise_next --topic bash

Practise this next

Turn the guide into practice with a related quiz, builder, cheat sheet or learning path.