Search code examples
bashcsh

How to catch /bin/bash: bad interpreter error


recently came across an issue when running a bash script executed in a csh shell. This was outputed: /bin/bash: bad interpreter: No such file or directory. The problem was bash was not on the environment path. After adding bash, this was fixed. I want to make sure that in the future, if this ever happened again for some reason, I can handle this. I am wonder what exit code this is? or is this just printed out on stderr? I want to catch this and fail the main script. Any ideas on how to handle this?

I have this segment:

bash sc142.sh

#####################################################################
# Check for processing errors
#####################################################################
if ($status != 0) then
    exit (-1)
endif

Solution

  • I tried this on Debian, the exit status for a bad interpreter error is 126. So you can do:

    /path/to/scriptname arg ...
    if ( $status == 126 ) then
        echo "scriptname failed"
        exit 1
    endif
    

    Note that a false positive is possible. If the last command in the script you're running exits with status 126, you won't be able to tell the difference.