macOS Error: /bin/bash: bad interpreter: Operation not permitted

I recently attempted to execute a Bash script in Terminal on my Mac but received an error message:

/bin/bash: bad interpreter: Operation not permitted

It turns out that this is often due to the "quarantine" filesystem metadata (extended attributes) being set for the file, preventing it from being executed.

To view the extended attributes for a file (called "my-file.sh"), execute the command:

xattr -l my-file.sh

You should see one of the attributes listed as being set as com.apple.quarantine. To remove it from the file, execute:

xattr -d com.apple.quarantine my-file.sh

If you view the extended attributes list again, you should now see the "quarantine" attribute is gone and you can execute the script.