6

I wrote a bash script under CentOS and it was executed well. On another computer it was wrong. I forgot the shebang at the beginning, but why was it good on my computer? I assume it's a very beginner question, but I gave it a try. Thanks.

Updated: Another question popped up. What's the difference between executing with ./filename.sh and sh filename.sh?

codeforester
  • 39,467
  • 16
  • 112
  • 140
Lgn
  • 9,581
  • 5
  • 20
  • 26
  • 1
    Did you run it with `sh script.sh`? In this case you won't need a shebang because the shell will already know which executable to run it with. – Lee Netherton Mar 30 '12 at 14:37
  • I used scriptname.sh to run it and it went well. Why? – Lgn Mar 30 '12 at 14:38
  • did you make it executable on the other system? try running it as `./scriptname.sh` instead of `scriptname.sh` – scibuff Mar 30 '12 at 14:39
  • You mean you ran it with `./scriptname.sh `? Or you put it somewhere in the $PATH? – Lee Netherton Mar 30 '12 at 14:39
  • I did not put it in the $PATH – Lgn Mar 30 '12 at 14:42
  • possible duplicate of [Bash script execution with and without shebang in Linux and BSD](http://stackoverflow.com/questions/7268437/bash-script-execution-with-and-without-shebang-in-linux-and-bsd) – sorpigal Mar 30 '12 at 14:49
  • 2
    *"On another computer it was wrong."* I presume that means the computer reached out and slapped you in the face when you ran the script. If not, please tell us what happened when you tried to run the script (exact error message if any, etc.) – Keith Thompson Mar 30 '12 at 14:56
  • It says _"Command not found"_. I assume it is what you all said, without shebang, I need to use sh. – Lgn Mar 30 '12 at 16:27
  • I think the accepted answer already answers you updated question. – Bernhard Apr 01 '12 at 19:09
  • So ./ will execute it with the previous running shell? – Lgn Apr 01 '12 at 19:12

2 Answers2

4

Not having a shebang in the beginning of your script will get it executed in whatever shell is currently running when the script was invoked. If you know for sure that the script will be launched from bash, and not from any other shell (ksh, csh, etc.), there is no need for a shebang, because the same interpreter will be launched.

Blagovest Buyukliev
  • 42,498
  • 14
  • 94
  • 130
  • also, I believe some systems use aliasing so calling `sh` may actually run a different shell – scibuff Mar 30 '12 at 14:40
  • 1
    This is not always true. I've seen shells that start `/bin/sh` rather than themselves as default. – Charles Duffy Oct 03 '17 at 21:23
  • 3
    `bash` is actually the only shell that starts itself. All other shells use `sh`, including `zsh`, `ksh`, `dash`, plus any tool using the `exec*p` family of functions. – that other guy Oct 03 '17 at 21:34
  • @thatotherguy, eh? the `exec*` functions typically just fail, rather than using `/bin/sh`, in the absence of any shebang. – Charles Duffy Oct 03 '17 at 22:27
  • 2
    @CharlesDuffy `exec*p` uses `sh`. Other non-`p` `exec*` functions will fail. It's documented [in POSIX](http://pubs.opengroup.org/onlinepubs/9699919799/functions/exec.html). – that other guy Oct 03 '17 at 22:50
4

If execve fails for your script, which it will, /bin/sh will be used. On one system /bin/sh may be a POSIX sh and on another it may be an alias for bash; your script probably relies on bash features.

sorpigal
  • 25,504
  • 8
  • 57
  • 75