Search code examples
macoszshshebang

Bash shebang is ignored - script is still executed with zsh


I have a script on Mac OSX which is executed when opening a new terminal. It is specified in .zprofile. It has a bash shebang but it is still executed with zsh (my default shell). What may be the problem here?

#!/bin/bash

The core problem is that I am not able to execute the script due to differences of zsh syntax. When I analysed what may cause this, I recognised that the shebang is just ignored.

I have a binary /bin/bash btw.

Edit:

~/.zprofile executes the script like so:

. ~/.script.bash

Solution

  • You have written in you ~/.zprofile the following line:

    . ~/.script.bash
    

    This is similar to

    source ~/.script.bash
    

    Which implies that your script is sourced and not executed. You should have something like:

    ~/.script.bash
    

    instead which will execute in the environment defined by the shebang. Note that the file needs to be executable.

    . file [ arg ... ]: Read commands from file and execute them in the current shell environment.

    source: man zshall