BeginnerPhone
1 min
Bash Shebang & Execute Bit
LinuxScriptingSystem Administration
Advertisement
Interview Question
What does the shebang line do in a script, and what permission is required to run it directly?
Key Points to Cover
- Shebang (e.g., #!/usr/bin/env bash) selects interpreter
- Executable bit (chmod +x) required to run as ./script.sh
- Alternatively: run via interpreter directly (bash script.sh)
Evaluation Rubric
Explains shebang purpose40% weight
Mentions execute permission30% weight
Shows interpreter invocation alternative30% weight
Hints
- 💡env helps locate interpreter on PATH.
Common Pitfalls to Avoid
- ⚠️**Missing Executable Permission:** Forgetting `chmod +x` will result in 'Permission denied' when trying to run `./script.sh`.
- ⚠️**Incorrect Shebang Path:** Specifying a wrong or non-existent path to the interpreter (e.g., `#!/bin/python` when it's at `/usr/bin/python3`) leads to 'bad interpreter' errors.
- ⚠️**Windows Line Endings (CRLF):** Creating a script on Windows and running it on Linux without converting line endings can cause 'bad interpreter' errors, as the interpreter path might include a hidden carriage return character.
- ⚠️**Shebang Not First Line:** If the shebang isn't the *absolute first line* of the script (e.g., preceded by a blank line or a comment), it will be ignored and the script might be executed by the default shell, leading to unexpected behavior.
- ⚠️**No Shebang:** Without a shebang, the shell running the script (e.g., `bash script.sh`) will typically use its default interpreter, which might not be the one intended by the script's author, potentially causing syntax errors or different execution logic.
Potential Follow-up Questions
- ❓How to pass arguments?
- ❓What about CRLF line endings issues?
Advertisement