Search code examples
bashmacosshellshfish

Failed to execute bash script on mac


I’ve created the following simple test script

#!/bin/bash

echo "test"

I use chmod +x ./msa.sh to execute it.

Now, when I try to run it like ./msa I got the following error,

Failed to execute process ‘./msa.sh'. Reason:
exec: Exec format error
The file './msa.sh' is marked as an executable but could not be run by the operating system.

I'm using mac with fish, what should I do in order to run it successfully?


Solution

  • Are you sure you're saving the script as a plain text file? If you're using TextEdit you may be saving a RTF. Choose Format > Make Plain Text and resave the file if needed.

    You can also examine the file from the shell to make sure it starts with the shebang line you're expecting. For example:

    nicholas@mary ~> file msa.sh
    msa.sh: Rich Text Format data, version 1, ANSI
    nicholas@mary ~> head -1 msa.sh
    {\rtf1\ansi\ansicpg1252\cocoartf1561\cocoasubrtf400
    

    That is not what you want.

    nicholas@mary ~> file msa.sh
    msa.sh: Bourne-Again shell script, ASCII text executable
    nicholas@mary ~> head -1 msa.sh
    #!/bin/bash
    

    And that is what you want.