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.
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.
#!/usr/bin/env bash
# ↑ This is the shebang line — always put it firstWhy /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
#!/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 echoprints text to the terminal$(date)runs thedatecommand and substitutes its output inline (command substitution)$USERand$HOMEare 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.shWhy 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:
#!/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 -1What does the shebang line `#!/usr/bin/env bash` tell the OS?
Create a script called greet.sh that:
- Prints "Good morning!" if the current hour is before 12
- Prints "Good afternoon!" if between 12 and 17
- 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.