Bash script: bad interpreter

83,346

Solution 1

The first line, #!/bin/bash, tells Linux where to find the interpreter. The script should also be executable with chmod +x script.sh, which it appears you did.

It is highly likely that you created this file with a windows editor, which will place a <cr><lf> at the end of each line. This is the standard under dos / windows. OS X will place a <cr> at the end of each line. However, under Unix / Linux, the standard is to just put a <lf> at the end of the line.

Linux is now looking for a file called /bin/bash<cr> to interpret the file, where <cr> is a carriage return character, which is a valid file character under Linux. Such a file doesn't exist. Hence the error.

Solution: Edit the file with an editor on Linux and get rid of the extra <cr>. One tool that usually works when the file is edited on Windows is dos2unix.

Solution 2

Could the script be using Dos newlines?

Try running dos2unix on it.

Solution 3

It looks like things have been configured to override the export builtin somehow. This can be done via an exported function or the enable builtin, for example. Try putting type export in the script to check. If you are setting BASH_ENV, you probably shouldn't.

If bash is called as sh, it enables POSIX mode and does not allow export to be overridden with a function, as required by POSIX. Likewise, most other shells installed as /bin/sh follow POSIX in this and/or do not allow the execution environment of a script to be messed up so strongly as through importing functions from the environment.

By the way, the script seems designed to be sourced, i.e. . ./mono-2.6-environment instead of ./mono-2.6-environment.

Solution 4

Had the same problem. Used brute force:

/bin/sh /full/path/to/configure --options

& this did the trick

(Of course I'd like to know why)

Share:
83,346
Stefan Steiger
Author by

Stefan Steiger

I'm an avid HTTP-header-reader, github-user and a few more minor things like BusinessIntelligence &amp; Web Software Developer Technologies I work with: Microsoft Reporting- &amp; Analysis Service (2005-2016), ASP.NET, ASP.NET MVC, .NET Core, ADO.NET, JSON, XML, SOAP, Thrift ActiveDirectory, OAuth, MS Federated Login XHTML5, JavaScript (jQuery must die), ReverseAJAX/WebSockets, WebGL, CSS3 C#, .NET/mono, plain old C, and occasional C++ or Java and a little Bash-Scripts, Python and PHP5 I have a rather broad experience with the following relational SQL databases T-SQL PL/PGsql including CLR / extended stored procedures/functions Occasionally, I also work with MySQL/MariaDB Firebird/Interbase Oracle 10g+ SqLite Access I develop Enterprise Web-Applications (.NET 2.0 &amp; 4.5) and interface to systems like LDAP/AD (ActiveDirectory) WebServices (including WCF, SOAP and Thrift) MS Federated Login OAuth DropBox XML &amp; JSON data-stores DWG/SVG imaging for architecture In my spare-time, I'm a Linux-Server-Enthusiast (I have my own Web &amp; DNS server) and reverse-engineer with interest in IDS Systems (IntrusionDetection), WireShark, IDA Pro Advanced, GDB, libPCAP. - Studied Theoretical Physics at the Swiss Federal Institute of Technology (ETHZ).

Updated on November 23, 2020

Comments

  • Stefan Steiger
    Stefan Steiger over 3 years

    Question: I get this error message:

    export: bad interpreter: No such file or directory

    when I execute this bash script:

    #!/bin/bash
    MONO_PREFIX=/opt/mono-2.6
    GNOME_PREFIX=/opt/gnome-2.6
    export DYLD_LIBRARY_PATH=$MONO_PREFIX/lib:$DYLD_LIBRARY_PATH
    export LD_LIBRARY_PATH=$MONO_PREFIX/lib:$LD_LIBRARY_PATH
    export C_INCLUDE_PATH=$MONO_PREFIX/include:$GNOME_PREFIX/include
    export ACLOCAL_PATH=$MONO_PREFIX/share/aclocal
    export PKG_CONFIG_PATH=$MONO_PREFIX/lib/pkgconfig:$GNOME_PREFIX/lib/pkgconfig
    PATH=$MONO_PREFIX/bin:$PATH
    PS1="[mono-2.6] \w @ "
    

    But the bash path seems to be correct:

    asshat@IS1300:~/sources/mono-2.6# which bash
    /bin/bash
    
    asshat@IS1300:~# cd sources/
    asshat@IS1300:~/sources# cd mono-2.6/
    asshat@IS1300:~/sources/mono-2.6# ./mono-2.6-environment
    export: bad interpreter: No such file or directory
    asshat@IS1300:~/sources/mono-2.6# ls
    download  mono-2.4  mono-2.4-environment  mono-2.6  mono-2.6-environment
    asshat@IS1300:~/sources/mono-2.6# cp mono-2.6-environment mono-2.6-environment.sh
    asshat@IS1300:~/sources/mono-2.6# ./mono-2.6-environment.sh
    export: bad interpreter: No such file or directory
    asshat@IS1300:~/sources/mono-2.6# ls
    download  mono-2.4-environment  mono-2.6-environment
    mono-2.4  mono-2.6              mono-2.6-environment.sh
    asshat@IS1300:~/sources/mono-2.6# bash mono-2.6-environment
    asshat@IS1300:~/sources/mono-2.6#
    

    What am I doing wrong? Or is this a Lucid Lynx bug?

    I did chmod + x