Bash script prints "Command Not Found" on empty lines
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.
I also ran into a similar issue. The issue seems to be permissions. If you do an ls -l
, you may be able to identify that your file may NOT have the execute bit turned on. This will NOT allow the script to execute. :)
As @artooro added in comment:
To fix that issue run
chmod +x testscript.sh