Interview › Scripting (Bash, Groovy)
What is the purpose of the shebang line, and what does #!/bin/bash do? [Basic]
Answer
The shebang tells the operating system which interpreter should run the script when it is executed directly. #!/bin/bash means: run this file using the Bash interpreter located at /bin/bash.
Technical explanation
The shebang must be the first line in the file; otherwise the kernel will not use it as the interpreter directive.
It matters when running ./script.sh. If I run bash script.sh, I am explicitly choosing Bash and the shebang is mostly ignored.
For portability, #!/usr/bin/env bash is often used when Bash may not live at /bin/bash, but production scripts should still document the required Bash version.
Hands-on example
Create and run a Bash script:
cat > hello.sh <<'EOF'
#!/bin/bash
echo "running with $BASH_VERSION"
EOF
chmod +x hello.sh
./hello.sh
Check how well your resume matches the role with our free resume checker— match score, ATS check, and the skills you're missing.
More Scripting (Bash, Groovy) interview questions
- What is the difference between sh and bash? [Basic]
- How do you make a script executable and run it? [Basic]
- What is the difference between running a script with ./script.sh, bash script.sh, and source script.sh? [Basic]
- What does sourcing a script do differently from executing it? [Basic]
- How do you declare a variable in Bash, and why are spaces around = not allowed? [Basic]
- What is the difference between $var and ${var}? [Basic]
- What is the difference between single quotes and double quotes in Bash? [Basic]
- Why should you quote variables, and what bug does unquoting cause? [Basic]