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:
#!/bin/bash echo "Hello, World!" echo "Today is:" date
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.
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:
chmod +x hello.sh ./hello.sh
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.
./ 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."