17

I want to go through the files in a directory with a for loop but this comes up.

echo: bad interpreter: No such file or directory

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
Alek
  • 229
  • 2
  • 4
  • 12
  • the first echo is just to show the directory the last one is for the counter – Alek Mar 31 '13 at 23:03
  • 1
    Did you actually copy/paste your code? What you have there has a syntax error on the `if` line. – Mat Mar 31 '13 at 23:05
  • just fixed the missing empty space.. same error occurs – Alek Mar 31 '13 at 23:10
  • [Don't use `ls`](http://mywiki.wooledge.org/ParsingLs) for this kind of file processing. Use [globbing](http://tldp.org/LDP/abs/html/globbingref.html) instead: `for file in *; do ...` – Ansgar Wiechers Mar 31 '13 at 23:43
  • faced the same issue. Created a new text file and renamed it to blah.sh.Edited it gvim to include #! /bin/sh and my commands.While runnin g on shell the "bad interpreter: No such file or directory" error was observed. Then deleted the file and created a similar file using vi editor in the shell and added the same set of line as in previous script and this time it worked without any fuss. – bharath May 03 '17 at 12:27
  • @bharath maybe something similar to this happened to you: https://stackoverflow.com/questions/2841593/bash-script-bad-interpreter/52442368#52442368 file looks ok but "Stale file handle" file system gone a bit funny. – gaoithe Sep 21 '18 at 14:21
  • Possible duplicate of [Bash script: bad interpreter](https://stackoverflow.com/q/2841593/608639) – jww Oct 19 '18 at 03:13

11 Answers11

24

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

I found another solution here. Change

#!/bin/bash

for

#!/usr/bin/bash

Marco Navarro
  • 345
  • 3
  • 4
  • This will lead to errors on other systems. For instance, your 2nd ["shebang line"](https://en.wikipedia.org/wiki/Shebang_(Unix)) doesn't work on Centos 6. – Ondra Žižka May 02 '18 at 22:12
8

Better do :

#!/bin/bash
count=0
dir="$PWD"
echo "$dir"

for file in "$dir"/*
do
 if [[ -f $file ]]
 then
  ((count++))
 fi
done
echo $count

or a simplest/shortest solution :

#!/bin/bash

echo "$PWD"

for file; do
 [[ -f $file ]] && ((count++))
done

echo $count
Gilles Quenot
  • 143,367
  • 32
  • 199
  • 195
7

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

About the #!... line

This line hints the shell what interpreter to use to run the file. That can be e.g. bash, or sh (which is (roughly) a subset so a lot of things won't work), or basically anything that can execute the file content - Perl, Python, Ruby, Groovy...

The line points the system in cases like calling the script directly when it's executable:

./myScript.sh

It is also often used by editors to recognize the right syntax highlighting when the file has no suffix - for instance, Gedit does that.

Solution

To override the line, feed the script to Bash as a parameter:

bash myScript.sh

Or, you can 'source' it, which means, from within a Bash shell, do either of

source myScript.sh
. myScript.sh

which will work (roughly) as if you pasted the commands yourself.

Ondra Žižka
  • 36,997
  • 35
  • 184
  • 250
  • Overriding our sourcing instead are both problematic. You want to fix the shebang so it points to the right place. Sourcing has different semantics and is often not safe unless you separately run it in a subshell. – tripleee Sep 02 '19 at 05:24
5

In my case the bash script was created on a Windows PC which added a carriage return character in front of every line feed. \x0D\x0A instead of just \x0A. I replaced all the CRLF with just LF using the sed and my script works now.

sed -i 's//\r/\n//\n/g' /path/to/file.sh
fahadash
  • 3,174
  • 1
  • 25
  • 52
1

That's a strange error to be getting. I recommend trying to find the source of the error.

One thing is to check the pwd command.

type pwd

Make sure it's /usr/bin/pwd or /bin/pwd, and verify it's not a script:

file /usr/bin/pwd

If it is a script, I bet it starts with

#!echo
ash
  • 4,078
  • 1
  • 17
  • 29
  • `pwd` is a bash builtin: `type -a pwd` – glenn jackman Aug 22 '13 at 23:47
  • Usually is, true. Something's causing the "echo: bad interpreter: No such file or directory". The only way I've ever seen "Bad interpreter" before is an exec of a shell script with a bad path in the `#!/path` line. – ash Aug 25 '13 at 03:55
1

If you did use Homebrew to install BASH,

Removing the #!/bin/bash will be suffice.

Mohamed Anees A
  • 3,039
  • 14
  • 28
  • Removing the shebang is very dubious advice. Maybe it needs editing, but completely removing it will have unintended consequences on many systems. – tripleee Sep 02 '19 at 05:26
1

You can find where bash is located using command

whereis bash

and you can copy the bash path to the path where you are seeing bad-interpreter error.

Jyoti Dhiman
  • 390
  • 1
  • 4
  • 13
1

I have just come across the same issue and found that my error was in my first line, having

#!bin/bash

instead of

#!/bin/bash
David
  • 21
  • 4
0

I have followed the steps from the following link and issue has been resolved.

Ref link: script error - bad interpreter Actually, there was an extra ^M symbol at the end of each line. So, after the removal of that, it worked fine for me.

Hope it helps!

0

This issue could also occur when the file is not in unix format.. try running dos2unix againts the file and try again.

Ren Roz
  • 13
  • 5
0

so, if you change your username group priority from username to root, you should change

#!/user/bin/bash

to

#!/bin/bash

check your user group (my username is rommi)

$ groups rommi

this command will output:

rommi : root adm cdrom sudo dip plugdev lxd lpadmin sambashare

since my username group priority is set to root, i should change my script to

#!/bin/bash

i change the priority group using:

sudo usermod -g root rommi

if groups rommi command outputs:

rommi : rommi adm cdrom sudo dip plugdev lxd lpadmin sambashare

then my script should use #!/usr/bin/bash

fail make this changes will resutl in bad interpreter: No such file or directory error

Rommi
  • 1
  • 1