1

What is first line written in shell script What is the meaning of that If I didn’t write that line what will happen Then how to run the script

Sravani Sras
  • 19
  • 1
  • 2
  • Related: [Which shell interpreter runs a script with no shebang?](https://unix.stackexchange.com/questions/373223/which-shell-interpreter-runs-a-script-with-no-shebang) – steeldriver Oct 12 '19 at 10:11
  • This Q is about scripts without shebang, not about execve and programs. –  Oct 12 '19 at 11:50
  • @rastafile amazingly enough, the answers to the linked question explain what happens where there is no shebang. – Stephen Kitt Oct 12 '19 at 11:51
  • I am not going to dig for that. –  Oct 12 '19 at 11:56

2 Answers2

3

The first line (called shebang) tells what kind of interpreter should be used for the execution of this file. The answer that is marked as "Related" by steeldriver will tell you in more details what happens.

So, if you start with

 #!/bin/bash

bash is used to interpret this file. Other common starters are #!/usr/bin/perl or #!/usr/bin/python. But it is not limited to that; if you create a file tst with

#!/usr/bin/vi
jantje zag eens pruimen hangen
oh als eieren zo groot

,make that executable (chmod +x tst) and exectute it (./tst), it will start-up vi to edit the file.

Next part of the question was, what happens if there is no shebang as first line. The answer is that the currently used interpreter will be used. As a demo: create a file tst with:

ps -f

(no shebang, just one line),make that executable (chmod +x tst) and exectute it (./tst). The result will be something like:

ljm@phi:~$ ./tst
UID        PID  PPID  C STIME TTY          TIME CMD
ljm       1379  1377  0 Oct06 pts/3    00:00:00 -bash
ljm      20769  1379  0 10:34 pts/3    00:00:00 -bash
ljm      20770 20769  0 10:34 pts/3    00:00:00 ps -f

(1379 is my current shell, 20769 is the bash that is used to interpret tst, and it is therefore the parent (PPID) of ps)

If I start a sh and execute tst again, I see the following:

ljm@phi:~$ sh
$ ./tst   
UID        PID  PPID  C STIME TTY          TIME CMD
ljm       1379  1377  0 Oct06 pts/3    00:00:00 -bash
ljm      20773  1379  0 10:34 pts/3    00:00:00 sh
ljm      20774 20773  0 10:34 pts/3    00:00:00 /bin/sh ./tst
ljm      20775 20774  0 10:34 pts/3    00:00:00 ps -f
$ 

So now, sh is used to interpret tst.

Ljm Dullaart
  • 4,142
  • 11
  • 26
  • Sometimes you might also see a shebang line with `/usr/bin/env` in it, e.g. `#!/usr/bin/env - perl`. This uses the PATH environment variable to find the desired script interpreter, i.e. `perl` in this example. I think there is a POSIX specification or some other strong convention that `env` is always in `/usr/bin`, so this can be helpful if your script needs to be portable to many unix-like OSs with different locations for e.g. the `perl` or `python` interpreters. – telcoM Oct 12 '19 at 11:03
  • @telcoM I don't think there's any POSIX specification or other convention that `env` is in `/usr/bin`. Using `env` in shenbang lines is just a cargo-cult trick to pretend that it's safe to look up an interpreter in PATH. –  Oct 12 '19 at 13:26
  • 1
    @pizdelect Maybe so, but I don't recall ever seeing `env` in anywhere but `/usr/bin`, and I've been using Linux, Solaris, HP-UX and AIX. Selective memory or happy accident? I agree that using it in the shebang line is not necessarily a good idea, but apparently some people use it. – telcoM Oct 12 '19 at 19:11
1

Then how to run a script without shebang?

By sourcing it as file (say tst), even non-executable:

source tst
. tst

(these are buitins -> help source)

or

bash tst

which is similar to

bash -c 'string of commands aka script'

This matching up of language interpreters and "command streams" also works with perl, sed and awk, in different ways.

  • You can run `./tst` directly, if it’s executable. – Stephen Kitt Oct 12 '19 at 11:52
  • but not if it is non-shell code –  Oct 12 '19 at 11:53
  • the Q askes: "Then how to run..." meaning then=without shebang –  Oct 12 '19 at 11:54
  • @StephenKitt I edited my FIRST line...should be clear now that I am just answering that last sub-Q. –  Oct 12 '19 at 11:59
  • i had "...even non-executable" all the time! So what's your problem? –  Oct 12 '19 at 12:01
  • @StephenKitt You see? I think sravani is some kind of benchmarking avatar. You really should not duplicate so many Qs to old confusing Qs (of your own) –  Oct 12 '19 at 12:06