Shell scripting: new line command not found

2019-06-06 11:10发布

I am trying to run my shell script from command line lets say;

my script looks like this:

#!bin/bash

echo hello

When try to run this source ./abcd.sh I get this error.

"' is not a typo you can run the following command to lookup the package that contains the binary:
    command-not-found 
: command not found
hello
"

Never seen this before something wrong with having a empty line before "echo hello" ? I was wondering if anyone else encountered something like this.

标签: shell
5条回答
Explosion°爆炸
2楼-- · 2019-06-06 11:22
#!bin/bash

needs to be

#!/bin/bash

or wherever bash is installed (you can locate this by doing whereis bash).

Your program should work fine when invoked using bash, i.e., bash ./abcd.sh, but when executed directly ./abcd.sh then the hashbang line does matter because that is how the interpreter is located for the script contained in the executable file.

查看更多
走好不送
3楼-- · 2019-06-06 11:24

Try echo 'hello', within quotes. It looks like there is a newline between the echo command and hello and it is trying to run 'hello' as a command.

The hashbang line should be #!/bin/bash, but messing that up won't matter as it will interpret any line that starts with a hash as a comment.

查看更多
疯言疯语
4楼-- · 2019-06-06 11:25

Along with the first line of your script being a comment, it sounds like your file has DOS line endings, and the carriage return is being treated as the command that isn't found. The error message sounds like something provided by a custom command_not_found_handle function (which I believe Ubuntu defines).

查看更多
女痞
5楼-- · 2019-06-06 11:29

Make sure your file does not have a BOM I had the same problem when editing a script under Windows with Notepad++. make sure to convert to "UTF-8 witout BOM".

查看更多
Lonely孤独者°
6楼-- · 2019-06-06 11:32

Run script with debug option to see which line actually is failing:

bash -x abcd.sh

Note: in this case the Shebang line will be treated as a comment, so if the rest of your script is correct, it will execute correctly:

$ bash -x abcd.sh
+ echo hello
hello
查看更多
登录 后发表回答