Bash script: bad interpreter
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,
<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
Bash script and /bin/bash^M: bad interpreter: No such file or directory
I have seen this issue when creating scripts in Windows env and then porting over to run on a Unix environment.
dos2unix on the script:
Or just rewrite the script in your Unix env using
vi and test.
Unix uses different line endings so can't read the file you created on Windows. Hence it is seeing ^M as an illegal character.
If you want to write a file on Windows and then port over, make sure your editor is set to create files in UNIX format.
In notepad++ in the bottom right of the screen, it tells you the document format. By default, it will say
Dos\Windows. To change it go to
- new document / default directory tab
- select the format as unix and close
- create a new document
What it means /bin/bash^M: bad interpreter?
It means your script file has MSWin line endings. Use
fromdos to fix them.
Running a Bash script results in 'Bad interpreter: No such file or directory' error
On your system, the
bash shell lives in
/bin/bash and not
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.
shell script: bad interpreter: No such file or directory when using pwd
Better do :
for file in "$dir"/*
if [[ -f $file ]]
or a simplest/shortest solution :
for file; do
[[ -f $file ]] && ((count++))
How to catch /bin/bash: bad interpreter error
I tried this on Debian, the exit status for a bad interpreter error is
126. So you can do:
/path/to/scriptname arg ...
if ( $status == 126 ) then
echo "scriptname failed"
Note that a false positive is possible. If the last command in the script you're running exits with status
126, you won't be able to tell the difference.
bin bash bad interpreter
Small errors in your script:
- $brojac is unassigned, so your integer comparison fails. Assign it an initial value to fix.
- '[' calls test, so you need spaces around opening and closing braces.
- You can't have spaces around your equal sign when assigning a value.
Your script, updated:
while [ $brojac -le 5 ]
brojac=$(( brojac+1 ))