1

I did found questions on

Bad interpreter: No such file or directory thing on SO.

My issue is also solved when I changed the script from

#!/usr/bin/bash
echo -e "\t\t\e[92mHello from the Test Script!\e[39m"

to:

#!/bin/bash
echo -e "\t\t\e[92mHello from the Test Script!\e[39m"

after I did the first line change from looking an answer here.

Shell script: Bad interpreter.No such file or directory

I can not understand why removing the /usr from the first line helps.

P.S.I am learning about linux file permissions and I was unable to execute my file even after changing the permission using '755'. So, please if anyone can explain me this.Thanks in advance.:)

Community
  • 1
  • 1
john400
  • 392
  • 4
  • 10
  • 20
  • 1
    Possible duplicate of [Bash script: bad interpreter](https://stackoverflow.com/q/2841593/608639) – jww Oct 19 '18 at 03:12
  • Why was this voted down? I have voted it up as although the english could be better, it is understandable enough. – HankCa Jan 27 '20 at 10:21

4 Answers4

2

On your system, the bash shell lives in /bin/bash and not /usr/bin/bash.

The path after the ! should be the path to an executable that will be passed the contents of the script as an argument.

You can read more about this at wikipedia

As for the second part of your question; it would not have mattered what the permissions are; as the file was pointing to a bad interpreter.

For more on unix file permissions, I suggest reading this entry on wikipedia.

Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
1

That's because there is no bash binary at /usr/bin/bash and the correct path for bash is /bin/bash.

The #! line at the top of scripts, called the shebang, determines what program (sh, bash, ruby, perl, python, etc.) is used for running the script.

This post covers this topic well:

https://unix.stackexchange.com/questions/87560/does-the-shebang-determine-the-shell-which-runs-the-script

Community
  • 1
  • 1
codeforester
  • 39,467
  • 16
  • 112
  • 140
1

In my case adding sh before script name solved the issue.

SkorpEN
  • 2,491
  • 1
  • 22
  • 28
  • What this did was override the shebang `#!/usr/bin/bash` (the shebang is used to specify the interpreter) and explicitly execute the script with the `sh`ell (`/bin/sh`). `which sh` will tell you that this is indeed the path. Similarly for `which bash`. – HankCa Jan 27 '20 at 10:25
0

You can also call your script by adding "./" at the beginning in case you call it from the local directory. The other solution is to call it by specifying its full path.

HelpBox
  • 55
  • 3
  • 1
    This will make no difference as the shebang (`#!/usr/bin/bash`) is still the (erroneous) interpreter of the script. This approach is used when you are in the directory a script is in. `$PATH` will not (**should not**) contain '.' (ie. the current directory). Thus with `./script.sh` you are saying "execute this script in the currrent directory". – HankCa Jan 27 '20 at 10:27