If I have both /usr/bin/perl and /usr/local/bin/perl available on a system, which one should I use?
- 829
2 Answers
It depends: on some systems, e.g. BSD packaging, /usr/local/bin may be the directory where packages install executables. In that case, it would be newer (and perhaps better), but /usr/bin/perl would be more stable.
You can use perl -V to see which has a newer version number. Sometimes the contents of /usr/local/bin (on multi-user systems) isn't really newer.
The usual compromise is to let the user decide, by using env in the shebang/hashbang line (but again, the path to env can vary by system).
The shebang line refers to the first line of a script, which may look like one of the following:
#!/usr/bin/perl
#!/usr/bin/perl -w
#!/usr/bin/env perl
The first two specify the directory from which to run Perl. As a bonus, this form allows passing an option, e.g. -w (used to turn on warnings). The last line uses the env program to pick the first occurrence of perl by searching the directories listed in the user's PATH environment variable.
It is also possible to run a script like this:
perl myscript
which (like env) tells the shell to pick the first occurrence of perl from the directories listed in PATH.
An explicit
./perl myscript
of course overrides the PATH. But scripts with shebang lines are the way most people use Perl scripts (Perl one-liners are a different matter).
Further reading:
- /usr/bin/env perl - query, referring to shebang line
- /usr/bin vs /usr/local/bin on Linux
- pkg(7), FreeBSD package system with
/usr/local/bin
- 76,765
Who is it for? If it is just you, as long as both your versions of perl are new enough it does not matter. Is it for a work environment? If so check your deployment policies as they may have the answer. If it is for distribution to the general public, I would recommend varying based upon package type, specifically use /usr/bin/perl for debs (per the debian guidelines) and rpms (as Red Hat specifically disallows rpms to install to /usr/local), but /usr/bin/env perl for tar and shar packages as it has the best chance of working on random unix computers.
- 5,829
- 3
- 31
- 43
perlfrom my home directory, whichperlwill be executed? – CJ7 Feb 15 '16 at 00:44type perlorcommand -v perlto find out – muru Feb 15 '16 at 01:11perlto be the one that is executed? – CJ7 Feb 15 '16 at 01:23PATHenvironment variable. – muru Feb 15 '16 at 01:24echo $PATHshould show you which location is used first. – schaiba Feb 15 '16 at 06:33tr : '\n' <<<":$PATH"– glenn jackman Feb 15 '16 at 17:19PATH, I use a script except when explaining howPATHworks. ymmv. – Thomas Dickey Feb 15 '16 at 18:16