Sagar.BlogArticle
All posts
All posts
Bash

Your First Bash Script — Hello World and Beyond

Write your first real Bash script from scratch. Learn the shebang line, echo, comments, and how to make a file executable.

January 2, 20266 min read
BashScriptingBeginner

Every Bash script starts with a single line that tells the OS what interpreter to use. From there, you write commands just like you would in the terminal. Let's build your first script step by step.

The Shebang Line

The very first line of every Bash script must be the shebang (pronounced "sha-bang") — a special comment that tells the OS which interpreter to use to run this file.

hello.sh
#!/usr/bin/env bash
# ↑ This is the shebang line — always put it first

Why /usr/bin/env bash?

You'll also see #!/bin/bash — this hardcodes the path to Bash. #!/usr/bin/env bash uses env to find Bash in the user's PATH, making your script work even if Bash is installed in a non-standard location (e.g. /opt/homebrew/bin/bash on macOS).

Prefer #!/usr/bin/env bash for portability.

Writing the Script

hello.sh
#!/usr/bin/env bash

# This is a comment — Bash ignores everything after #
# Use comments to explain WHY, not just what

echo "Hello, World!"
echo "Today is: $(date)"
echo "You are logged in as: $USER"
echo "Your home directory is: $HOME"

What each line does:

  • Lines starting with # are comments — ignored by Bash, read by humans
  • echo prints text to the terminal
  • $(date) runs the date command and substitutes its output inline (command substitution)
  • $USER and $HOME are environment variables automatically provided by the shell

Making It Executable and Running It

# 1. Create the file
nano hello.sh     # type the script content, then Ctrl+O to save, Ctrl+X to exit

# 2. Make it executable (only needed once per script)
chmod +x hello.sh

# 3. Run it
./hello.sh

Why the ./ prefix?

When you type a command, Bash looks for it in your $PATH directories. Your current folder (.) is NOT in PATH by default (for security reasons). The ./ prefix tells Bash explicitly: "look in the current directory".

You can also run a script without making it executable by passing it directly to Bash:

bash hello.sh      # run with bash interpreter directly
sh hello.sh        # run with POSIX sh (less features)

A More Useful Script

Let's write something that actually does work — a simple system info script:

sysinfo.sh
#!/usr/bin/env bash

echo "============================="
echo "  System Information Report  "
echo "============================="
echo ""
echo "Hostname:    $(hostname)"
echo "OS:          $(uname -o 2>/dev/null || uname -s)"
echo "Kernel:      $(uname -r)"
echo "Uptime:      $(uptime -p 2>/dev/null || uptime)"
echo "Date/Time:   $(date)"
echo ""
echo "CPU:         $(nproc) cores"
echo "Memory:"
free -h 2>/dev/null || vm_stat 2>/dev/null | head -5
echo ""
echo "Disk usage (/):"
df -h / | tail -1
Quick Check

What does the shebang line `#!/usr/bin/env bash` tell the OS?

Exercise

Create a script called greet.sh that:

  1. Prints "Good morning!" if the current hour is before 12
  2. Prints "Good afternoon!" if between 12 and 17
  3. Prints "Good evening!" otherwise

Hint: Use $(date +%H) to get the current hour as a number (00-23). You'll need an if statement — don't worry about the exact syntax yet, just get the time and print it.