Introduction

A shell script is just a plain text file full of the same commands you'd type one by one at a terminal prompt — saved so you can run all of them, in order, with a single command instead of retyping them every time.

What a shell script actually is

Every line in a .sh file is a command exactly as you'd type it into a terminal. Bash reads the file top to bottom and runs each line the same way it would if you'd typed it yourself, one after another.

The shebang line

The very first line of a script tells the system which program should run it. For Bash scripts, that's always:

bash hello.sh
#!/bin/bash
echo "Hello, World!"
echo "Today is:"
date
Terminal output
Hello, World!
Today is:
Tue Sep  8 14:02:11 UTC 2026

#!/bin/bash is called a shebang (from "hash-bang," the two characters it starts with). It's not a comment even though it starts with # — it's an instruction to the operating system, telling it to hand the rest of the file to /bin/bash for execution.

Note: the shebang has to be the very first line of the file — no blank line, no comment, nothing before it. If anything comes first, the system won't recognize it as a shebang and will try (and fail) to run the file some other way.

Making a script executable

A new script isn't runnable by default. chmod +x grants it execute permission, after which you can run it with ./ in front of its name:

$ terminal
chmod +x hello.sh
./hello.sh
Terminal output
Hello, World!
Today is:
Tue Sep  8 14:02:11 UTC 2026

You can also run a script without making it executable at all, by handing it directly to bash: bash hello.sh. That's useful for testing a script you don't own, or don't want to chmod.

Note: the ./ in front of ./hello.sh matters. Just typing hello.sh won't work on most systems, because your current directory usually isn't in your shell's search path ($PATH) for security reasons — ./ explicitly says "run the file right here."