Not found error when running a script with `sh`

I have a simple shell script:

#!/bin/bash
var="foo"
echo $var

When I run it, using sh shell.sh (shell being its filename, I get the following error:

: not found: 2: shell.sh:

What does this mean?

2

1 Answer

Have you tried sh ./shell.sh instead of sh shell.sh ?
(for security reasons, the current directory . is NEVER in the PATH varable)

Also the first line in your script #!/bin/bash is only needed if the script is run by itself,
like ./shell.sh (don't forget to do chmod a+x shell.sh in that case)

Finally the sh command invokes the dash shell, not the bash shell, so it would be wise to invoke bash scripts like bash ./shell.sh instead of sh ./shell.sh

Take care with line endings:
Bash does not accept \r\n (CRLF) as line endings (which is windows style).
Only \n (LF) is permitted.

5

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like