3

In Linux, we usually add a shebang in a script to invoke the respective interpreter. I tried the following example.

I wrote a shell script without a shebang and with executable permission. I was able to execute it using ./. But if I write a similar python program, without shebang, I am not able to execute it.

Why is this so? As far as my understanding, shebang is required to find the interpreter. So how does shell scripts work, but not a python script?

shar
  • 1,988
  • 2
  • 18
  • 25
  • I haven't investigated this, but I would suppose (and it would be logical that) it may default to interpreting it as a shell script (no other special interpreter) – lurker Sep 23 '13 at 16:43
  • It's out of spec. I'm reasonably sure that the interpreter will just give it a shot and see if it runs with itself (that is, `/bin/bash` will try with `/bin/bash`, `/bin/sh` will try with `/bin/sh`, etc.). – zebediah49 Sep 23 '13 at 16:44
  • 2
    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) –  Sep 23 '13 at 16:46

5 Answers5

5

My assumption is that a script without a shebang is executed in the current environment, which at the command line is your default shell, e.g. /bin/bash.

pjmorse
  • 9,204
  • 9
  • 54
  • 124
  • So if my current shell is a python shell, will a python script without a shebang work? – shar Sep 23 '13 at 16:49
  • 1
    As Joran Beasley points out, there is no python shell, and if there was, who knows what it would do. Most `sh`-like shells try to execute the script as themselves in the absence of a shebang, so if a hypothetical python shell did that, sure, a python script without a shebang might work. – pjmorse Sep 23 '13 at 16:51
4

shell scripts will only work if you are in the shell you targeted ... there is not python shell ... as such python will never work without explicity calling python (via shebang or command line)

Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
3

By default the shell will try to execute the script. The #! notation came later

Andres Olarte
  • 4,380
  • 3
  • 24
  • 45
2

There’a subtle distinction here. If the target is a binary or begins with a #! shebang line, then the shell calls execv successfully. If the target is a text file without a shebang, then the call to execv will fail, and the shell is free to try launching it under /bin/sh or something else.

Josh Lee
  • 171,072
  • 38
  • 269
  • 275
0

http://en.wikipedia.org/wiki/Shebang_(Unix)

Under Unix-like operating systems, when a script with a shebang is run as a program, the program loader parses the rest of the script's initial line as an interpreter directive; the specified interpreter program is run instead, passing to it as an argument the path that was initially used when attempting to run the script.

Now when the #! is not found, every line is interpreted as native shell command. And hence if you write a bash script and run it under bash shell it will work. If you run the same bash script in say a tcsh shell it will not work without the initial #!/usr/bin/tcsh

Kranthi
  • 83
  • 5