Howto Make Script More Portable With #!/usr/bin/env As a Shebang
You may have noticed that most shell and perl script starts with the following line:
#!/bin/bash
OR
#!/usr/bin/perl
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
#!/bin/bash
Replace with
#!/usr/bin/env bash
For example here is a small script:
#!/usr/bin/env bash
x=5
y=10
echo "$x and $y"
OR
#!/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 ![]()
Want to stay up to date with the latest Linux tips, news and announcements? Subscribe to our free e-mail newsletter or full RSS feed to get all updates.
You can Email this page to a friend.
You may also be interested in...
- Python Network Programming howto
- Linux / UNIX: Python programming tutorial for system administrators
- Tutorial : Building Web Pages with Python
- Improve shell program efficiency with Bash Dynamically Loadable
- Linux automated GUI testing with python based Dogtail tool
Discussion on This Article:
Leave a Reply
We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!
Tags: #!/usr/bin/env, environment variable, exclamation point, foolproof method, hack, hashbang, hashpling, interpreter, Linux, operating system, path environment, perl script, pound bang, scripts, shebang, symbolic link, UNIX ~ Last updated on: February 21, 2008


I am confused.
Why is /usr/bin/env more portable than /bin/bash.
Besides on most linux distros bash is found in /bin, but env is not guaranteed to be found under /usr/bin.
e.g. in my case (FC5) env is under /bin.
My point is , which ever way you need to know the absolute path to either env, or bash, so why bother ?
If you move from Linux distro to BSD you will see bash is located at /usr/local/bin/bash OR to Solaris you will see bash at /opt or some other location. Instead of adjusting all the location admin can create a /usr/bin/env softlink and problem solved. Just imagine you have 100s of shell and perl scripts…
This is not just about Linux. It is about running a script under different UNIX like oses.
Thanks for “shebang” and its explanation. Its really helpful.
The only problem I have with env, that on some systems lacking ‘use warnings’, I can’t pass ‘-w’ on command line. Or is it possible somehow?
You can pass /bin/path/to/mybinary -w