Bash script always prints “Command Not Found”

2019-01-01 10:41发布

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?

16条回答
只靠听说
2楼-- · 2019-01-01 11:21

use dos2unix on your script file.

查看更多
唯独是你
3楼-- · 2019-01-01 11:23

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.

查看更多
弹指情弦暗扣
4楼-- · 2019-01-01 11:24

Problems with running scripts may also be connected to bad formatting of multi-line commands, for example if you have a whitespace character after line-breaking "\". E.g. this:

./run_me.sh \ 
--with-some parameter

(please note the extra space after "\") will cause problems, but when you remove that space, it will run perfectly fine.

查看更多
怪性笑人.
5楼-- · 2019-01-01 11:25

You may want to update you .bashrc and .bash_profile files with aliases to recognize the command you are entering.

.bashrc and .bash_profile files are hidden files probably located on your C: drive where you save your program files.

查看更多
其实,你不懂
6楼-- · 2019-01-01 11:26

for executing that you must provide full path of that for example

/home/Manuel/mywrittenscript
查看更多
零度萤火
7楼-- · 2019-01-01 11:28

On Bash for Windows I've tried incorrectly to run

run_me.sh 

without ./ at the beginning and got the same error.

For people with Windows background the correct form looks redundant:

./run_me.sh
查看更多
登录 后发表回答