Bird
0
0

Which shebang line should you use to ensure it always runs with Bash when executed directly?

hard🚀 Application Q15 of 15
Bash Scripting - Basics
You want to write a script that runs with Bash but also works if run with sh script.sh (without executable permission). Which shebang line should you use to ensure it always runs with Bash when executed directly?
A#!/usr/bin/env bash
B#!/bin/bash
C#!/bin/sh
D#!/usr/local/bin/bash
Step-by-Step Solution
Solution:
  1. Step 1: Understand shebang with env

    #!/usr/bin/env bash uses the env command to find Bash in the user's PATH, making it portable.
  2. Step 2: Compare with fixed paths

    /bin/bash may not exist on all systems, and /bin/sh is often not Bash. /usr/local/bin/bash is less common.
  3. Step 3: Ensure script runs with Bash when executed directly

    Using #!/usr/bin/env bash ensures the script runs with Bash regardless of its location.
  4. Final Answer:

    #!/usr/bin/env bash -> Option A
  5. Quick Check:

    Use env to find Bash for portability [OK]
Quick Trick: Use #!/usr/bin/env bash for portable Bash scripts [OK]
Common Mistakes:
MISTAKES
  • Using /bin/sh which may not be Bash
  • Assuming /bin/bash exists everywhere
  • Ignoring PATH differences on systems

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes