1

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
  • Is the search path correct? Beacuse just entering the filename wont work. You nees the full path from your homefolder or from the root folder.
    – Alvar
    Nov 2, 2013 at 19:48
  • the directory my terminal is in is where the sh file is.
    – James T
    Nov 2, 2013 at 20:53

1 Answer 1

3

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
  • using bash invokes further errors, "./shell.sh: line 2: $'\r': command not found"
    – James T
    Nov 2, 2013 at 20:47
  • 2
    Dear James, don't edit Linux files under Windows. Windows' line endings (CRLF) are not compatible with the rest of the world (LF). The error you see is that you are running a windows formatted textfile under Linux and indeed CR is not a bash command.
    – thom
    Nov 2, 2013 at 20:54
  • @thom You should this last comment as answer. Nov 2, 2013 at 21:00
  • i had a major feeling that would be my problem, but i was hoping notepad++ would handle it.
    – James T
    Nov 2, 2013 at 21:06
  • No worries :-) If you are used to notepad++ just switch over to geany (which is native linux). It has the same engine and works almost the same...you'll love it :-)
    – thom
    Nov 2, 2013 at 21:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .