Interview Prep/Shell Scripting/Shells, scripts and execution

What does the shebang line do, and why prefer `#!/usr/bin/env bash` over `#!/bin/bash`?

BeginnerConceptShells, scripts and execution
Answer

When you run ./script.sh, the kernel reads the first line; #! followed by a path tells it which interpreter to exec, passing the script path as an argument. #!/bin/bash hard-codes a location. #!/usr/bin/env bash searches PATH, which picks up a newer Bash on macOS (Homebrew's Bash 5 instead of Apple's 3.2) or NixOS where /bin/bash does not exist.

Trade-off: env uses whatever bash is first on PATH, and on Linux you cannot pass more than one argument reliably (#!/usr/bin/env bash -e breaks). Put options in set lines instead.

All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on Shells, scripts and execution