The Shebang Line & Running Scripts
In this page:
Anatomy of the Shebang
The shebang #!/bin/bash tells the operating system's loader which interpreter to hand the rest of the file to when it is executed directly. It must be the first two characters of the file with no leading blank lines or spaces, otherwise it is just treated as an ordinary comment.
Note: $0 holds the script's own filename/path, which is why head -n 1 "$0" prints the shebang line back out.
Example: Anatomy of the Shebang
#!/bin/bash
echo "This script's interpreter is chosen by the first line"
head -n 1 "$0"
Login to try C/C++/Java/PHP code in the editor
Making a Script Executable
Before you can run a script as ./script.sh, the filesystem must mark it executable using chmod +x script.sh. Without that permission bit set, the kernel refuses to execute the file directly and reports 'Permission denied', even though the file's contents are perfectly valid Bash.
Example: Making a Script Executable
#!/bin/bash
cat <<'EOF' > demo.sh
#!/bin/bash
echo "hello from demo.sh"
EOF
chmod +x demo.sh
ls -l demo.sh
./demo.sh
Login to try C/C++/Java/PHP code in the editor
./script.sh vs bash script.sh
Running ./script.sh asks the kernel to execute the file directly, which requires the execute bit and relies on the shebang to choose the interpreter. Running bash script.sh instead starts Bash explicitly and hands it the file as an argument, so no execute permission or shebang is strictly required, but the shebang is still good practice for consistency.
Example: ./script.sh vs bash script.sh
#!/bin/bash
cat <<'EOF' > greet.sh
echo "hi, no shebang needed for this method"
EOF
bash greet.sh
chmod +x greet.sh
echo '#!/bin/bash' | cat - greet.sh > greet2.sh
chmod +x greet2.sh
./greet2.sh
Login to try C/C++/Java/PHP code in the editor
Checking Which Bash Is Used
#!/usr/bin/env bash looks up bash using the PATH environment variable rather than hard-coding /bin/bash, which matters on systems where Bash lives elsewhere (like some macOS or BSD setups). which bash or type bash shows exactly which binary would be used.
Warning: Hard-coding #!/bin/bash is fine on most Linux systems, but #!/usr/bin/env bash is more portable across different Unix-like systems.
Example: Checking Which Bash Is Used
#!/bin/bash
which bash
type bash
echo "Running under: $BASH"
Login to try C/C++/Java/PHP code in the editor
- Forgetting that the shebang must be the very first line, first two characters (
#!), with nothing before it -- even a blank line above it breaks it. - Trying to run
./script.shwithout first making it executable withchmod +x, then being confused by a 'Permission denied' error. - Assuming
./script.shandbash script.shalways behave identically; if the shebang is missing or wrong,./script.shmay use a different interpreter than you expect, whilebash script.shalways forces Bash regardless of the shebang.
- The shebang line has the exact form
#!/bin/bashor#!/usr/bin/env bashas the very first line of the file. chmod +x script.shgrants execute permission so the file can be run directly as./script.sh../script.shuses the shebang to pick the interpreter;bash script.shalways uses Bash and does not require execute permission or a shebang.#!/usr/bin/env bashis more portable across systems where Bash may not be installed at exactly/bin/bash.
Chapter Quiz — Complete all 12 topics to unlock
0/12 topics done
Complete these topics first: