1

shebang/hashbang, (that is, "#!") at the beginning of a script is an interpreter directive. This line tells the operating system what interpreter to invoke to run the script.

#!path/to/script

example:

which perl
perl is /opt/local/bin/perl

On my system perl is installed at above path, but not necessary, every system will have same path. Many time path varies from system to system, how do make it a generic one, so that I will not have to change the first line every time differing with machine to machine.

Nitin Tripathi
  • 1,224
  • 7
  • 17
  • 3
    How about `#!/usr/bin/env perl`? At least that is suggested in [What should I use for a Perl script's shebang line?](http://stackoverflow.com/a/2792076) – user000001 Sep 19 '15 at 04:24
  • is this generic to all scripts(Perl, Python, bash, tcsh, awk etc)? If Yes, then YES, i am looking for the same. – Nitin Tripathi Sep 19 '15 at 04:37
  • It is more or less generic; you write the command you want to run after `#!/usr/bin/env`. – Jonathan Leffler Sep 19 '15 at 04:39
  • @JonathanLeffler: Why is it more or less generic, and how does it work? – Nitin Tripathi Sep 19 '15 at 05:17
  • 1
    It is generic if you don't consider changing `python` to `perl` to `tcl` to `awk` as making it not-quite-generic. You need to get the name after `/usr/bin/env` right — that stops it being generic. However, the use of `#!/usr/bin/env` is as portable as it gets. How does it work? The [`env`](http://pubs.opengroup.org/onlinepubs/9699919799/utilities/env.html) command has a number of uses. It can print the environment, modify the environment before running a command, or it can simply run a command without modifying the environment — but the command is found on `$PATH`, which is the key point. – Jonathan Leffler Sep 19 '15 at 05:24
  • Agreed!! Thanks many!! – Nitin Tripathi Sep 19 '15 at 05:25

0 Answers0