≡ Menu

symbolic link

FAQ Updates: June/11/2012

Our FAQ section has been updated. Here are latest howtos:

Make the most of nixCraft FAQ Section

Get intimated about our new howtos / faqs as soon as it is released via RSS feed.

Not our fan yet?

Follow us on twitter OR join our facebook fan page for more tips and news about Linux and open source.

You may have noticed that most shell and perl script starts with the following line:

It is called a shebang. It consists of a number sign and an exclamation point character (#!), followed by the full path to the interpreter such as /bin/bash. All scripts under UNIX and Linux execute using the interpreter specified on a first line.

However there is a small problem. BASH or Perl is not always in the same location (read as PATH) such as /bin/bash or /usr/bin/perl. If you want to make sure that script is portable across different UNIX like operating system you need to use /usr/bin/env command.

env command allows to run a program in a modified environment.

Find line

Replace with
#!/usr/bin/env bash

For example here is a small script:

#!/usr/bin/env bash
echo "$x and $y"


#!/usr/bin/env perl
use warnings;
print "Hello " x 5;
print "\\n";

Now you don’t have to search for a program via the PATH environment variable. This makes the script more portable. Also note that it is not foolproof method. Always make sure you have /usr/bin/env exists or use a softlink/symbolic link to point it to correct path. And yes your work (script) looks more professional with this hack :)