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
The
echo: bad interpreter: No such file or directory
is most likely coming from the first line,#!...
which is called shebang line.About the
#!...
lineThis line hints the shell what interpreter to use to run the file. That can be e.g.
bash
, orsh
(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:
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:
Or, you can 'source' it, which means, from within a Bash shell, do either of
which will work (roughly) as if you pasted the commands yourself.
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.
Make sure it's /usr/bin/pwd or /bin/pwd, and verify it's not a script:
If it is a script, I bet it starts with
If you did use Homebrew to install BASH,
Removing the
#!/bin/bash
will be suffice.You can find where bash is located using command
and you can copy the bash path to the path where you are seeing bad-interpreter error.
Better do :
or a simplest/shortest solution :
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