Linux – Bad interpreter error when executing shell script with the correct shebang line

linuxshell-script

When executing a script from the path (scriptname, rather than "sh scriptname") I receive the following error:

-bash: ./gitup: /bin/bash/: bad interpreter: Not a directory

All of the other scripts in the same directory execute without issue, and I do not encounter this issue when the script is executed manually (sh scriptname), only when executed by itself. So the script, itself, seems to not be the issue as it does execute normally, and did so until I opened a new shell session and encountered this error for this script alone.

The script is executable, includes all requisite lines to execute in the way I've described (it worked properly until earlier today) and is able to be executed by my user, so I am unsure of what the issue could be.

The shebang line reads #!/bin/bash. I used the command "export PATH=$PATH:repos/utilities
" to direct to the directory storing these scripts, all of which, besides this one, execute simply by calling the script name (i.e. gitup).

Best Answer

There are a few things I can think of.

  1. You said your script is in your $PATH, yet you call it with its full path (./gitup as opposed to just gitup). Could it be that you have edited one version of the script (the one in your $PATH, say ~/bin/gitup) and have then executed another (./gitup)?

  2. Could there be multiple versions in your $PATH? You can check with

    which -a gitup
    
  3. You may have inadvertently entered a strange invisible character while editing the script. Check the shebang line using od:

    head -1 ./gitup | od -c
    

    Make sure there is nothing after #!/bin/bash but the newline character (\n). It should look like this:

    0000000   #   !   /   b   i   n   /   b   a   s   h  \n
    0000014
    
Related Question