Shell script: bad interpreter: no such file or directory when using pwd

I want to view files in a directory with a for loop, but this happens.

echo: bad interpreter: No such file or directory 

the code:

 #!/bin/bash count=0 dir=`pwd` echo "$dir" FILES=`ls $dir` for file in $FILES do if [ -f $file ] then count=$(($count + 1)) fi done echo $count 
+14
unix bash pwd
source share
9 answers

Better to do:

 #!/bin/bash count=0 dir="$PWD" echo "$dir" for file in "$dir"/* do if [[ -f $file ]] then ((count++)) fi done echo $count 

or the simplest / shortest solution:

 #!/bin/bash echo "$PWD" for file; do [[ -f $file ]] && ((count++)) done echo $count 
+6
source share

I had the same problem. Removing #!/bin/bash did the trick for me. It seems that there is no need to add where bash is located, since it is on the way to the system.

I found another solution here . Change

#!/bin/bash

for

#!/usr/bin/bash

+21
source share

echo: bad interpreter: No such file or directory , most likely from the first line, #!... which is called the Shebang line .

About the line #!...

This line tells the shell which interpreter to use to run the file. It could be, for example, bash or sh (which (approximately) is a subset, so many things will not work), or basically everything that the contents of a file can do - Perl, Python, Ruby, Groovy ...

The line tells the system in cases where you call the script directly when it is executed:

 ./myScript.sh 

It is also often used by editors to recognize the correct syntax highlighting when a file does not have a suffix - for example, Gedit does this.

Decision

To override a string, pass the script to Bash as a parameter:

 bash myScript.sh 

Or, you can "find" it, which means, from within the Bash shell, do any of

 source myScript.sh . myScript.sh 

which will work (approximately), as if you inserted the commands yourself.

+3
source share

In my case, the bash script was created on a Windows PC that added a carriage return character before each line feed. \ x0D \ x0A instead of just \ x0A. I replaced all CRLFs only with LF, using sed and my script now works.

 sed -i 's//\r/\n//\n/g' /path/to/file.sh 
+2
source share

This is a strange mistake. I recommend trying to find the source of the error.

One thing is to check the pwd command.

 type pwd 

Make sure / usr / bin / pwd or / bin / pwd, and do not check its script:

 file /usr/bin/pwd 

If this is a script, I'm sure it starts with

 #!echo 
+1
source share

I just ran into the same problem and found that my error was on my first line, having

 #!bin/bash 

instead

 #!/bin/bash 
+1
source share

If you used Homebrew to install BASH,

Removing #!/bin/bash will suffice.

0
source share

You can find where bash is with the command

 whereis bash 

and you can copy the bash path to the path where you see a bad interpreter error.

0
source share

I followed the instructions at the following link and the problem was resolved.

Link: script error - poor interpreter Actually, at the end of each line there was an extra character ^ M. So, after deleting it worked fine for me.

Hope it helps!

0
source share

All Articles