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,
where <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 dos2unix
.
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.
Try running dos2unix
on the script:
http://dos2unix.sourceforge.net/
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
- settings->preferences
- 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 dos2unix
or 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 /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.
shell script: bad interpreter: No such file or directory when using pwd
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
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"
exit 1
endif
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:
#!/bin/bash
mkdir ~/folder
brojac=0
while [ $brojac -le 5 ]
do
mkdir ~/folder/zad"$brojac"
brojac=$(( brojac+1 ))
done
Related Topics
Error When Using Git Credential Helper With Gnome-Keyring as Sudo
Automating Running Command on Linux from Windows Using Putty
Can Awk Patterns Match Multiple Lines
Why Is a Tilde in a Path Not Expanded in a Shell Script
Shell Command to Tar Directory Excluding Certain Files/Folders
How to Show All Shared Libraries Used by Executables in Linux
How to Setup & Run Phantomjs on Ubuntu
How to Insert a Text At the Beginning of a File
Run an Untrusted C Program in a Sandbox in Linux That Prevents It from Opening Files, Forking, etc.
How to Set Linux Environment Variables With Ansible
What Is the Maximum Size of a Linux Environment Variable Value
How to Access the System Call from User-Space