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

Preparing for an interview?

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

← All Scripting (Bash, Groovy) questions