ShBash · Lesson 1 of 8

Your First Script

A Bash script is just a text file with commands in it. The magic is in the shebang line at the top, which tells the system which interpreter to use.

The shebang (#!) on line 1 tells the OS what program to use to run the file. #!/bin/bash means "run this with Bash." Make the file executable with chmod +x, then run it with ./filename.sh.

Bash
#!/bin/bash
# This is a comment
# Above is the "shebang" — tells the OS to use bash

echo "Hello, World!"
echo "My name is $(whoami)"    # $() runs a command and inserts its output
echo "Today is $(date +%Y-%m-%d)"
echo "You are in: $PWD"        # $PWD is a built-in variable
Bash
# Save as hello.sh, then:
chmod +x hello.sh   # make it executable
./hello.sh          # run it

# Or without chmod:
bash hello.sh

# Useful echo options
echo "No newline" -n           # -n: no trailing newline
echo -e "Tab:\there"          # -e: interpret escape sequences
echo -e "Line1\nLine2"        # \n = newline

# printf — more control than echo
printf "Name: %s, Age: %d\n" "Alice" 30
printf "Pi: %.4f\n" 3.14159
◆ Note
Use #!/usr/bin/env bash instead of #!/bin/bash for better portability — it finds Bash wherever it is in PATH, rather than assuming it's at /bin/bash.