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.
More on Shells, scripts and execution
What is a shell, and how is Bash different from sh, dash and zsh?BeginnerWhat is the difference between `./script.sh`, `bash script.sh` and `source script.sh`?BeginnerWhat happens when you type a command name? How does Bash decide what to run?AdvancedWhy must `cd`, `export` and `read` be builtins rather than external programs?IntermediateWhat is the difference between a login, interactive and non-interactive shell, and which startup files run?IntermediateWhat does `exec` do in a script? Give two uses.Advanced