David David - 5 months ago 20
Linux Question

bash script always prints "Command Not Found"

Every time I run a script using

bash scriptname.sh
from the command line in Debian, I get
Command Not found
and then the result of the script. So the script works but there is always a
Command Not Found
statement printed on screen.

I am running the script from the
/var
folder.

Here is the script:

#!/bin/bash

echo Hello World


I run it by typing the following:

bash testscript.sh


UPDATE - the problem appears to the blank lines. Each blank line is resulting in a command not found. Why would this occur?

Answer

Make sure your first line is:

#!/bin/bash

Enter your path to bash if it is not /bin/bash


Try running:

dos2unix script.sh

That wil convert line endings, etc from Windows to unix format. i.e. it strips \r (CR) from line endings to change them from \r\n (CR+LF) to \n (LF).

More details about the dos2unix command (man page)


Another way to tell if your file is in dos/Win format:

cat scriptname.sh | sed 's/\r/<CR>/'

The output will look something like this:

#!/bin/sh<CR>
<CR>
echo Hello World<CR>
<CR>

This will output the entire file text with <CR> displayed for each \r character in the file.


P.S. Sorry for the poor formatting, this was written from my iPhone while driving to work :p.

Comments