Why do Perl 5 backtick (` `) calls fail on Linux only?

Well, I don't really know what to make of this.  I have run this script on 3 different RedHat boxes, all with the same result.  The same script obviously runs fine on any other Unixy type interp(AIX, HP, Solaris, Windows BASH), but SHOULD run on Linux bash or any other shell for that matter...even if you get a failure.


$binpath = `type perl`;  #don't really care if we get the parsed path here...just want SOMETHING
print "binpath = $binpath\n";


I just don't get it.   This SHOULD work and is very simple.    It works from the CLI, and I am using ActiveState 5.8.6.  It does the same thing on 5.6.  Please don't spend any unnecessary time on this, but if you have any ideas off the top of your head...

I am using RedHat 3.0 AS.

Who is Participating?
The 'type' command is a builtin in only some shells. Unless you have a separate command named 'type' in your PATH, perl is going to look at that string and, seeing no shell metacharacters, try to exec a command named 'type', and silently fail.
Did you want `which perl` ?
Your script could not have worked on AIX, HP, Solaris, unless you had a script or binary called 'type', and that isn't a standard command on any of those Unix flavours.

jmcg has given you the reason why it doesn't work.

What are you actually trying to acheive?
jchristenaAuthor Commented:
It did work on the other platforms, and I think I have a reason for it.  The default path on my Linux boxes consists of essentially /usr/sbin and thats it.  The other boxes contain /usr/bin and /bin by default which contain the bash and sh binaries.  If I explicitly specify a shell:

$var = `bash -c \"type perl\"`;

everything works fine.    In the end, it comes down to the fact that jmcg is correct.  Thanks for all the responses.

That seems like an odd default path to have. Lots of stuff is not going to work if you can only call on commands present in /usr/sbin.

My favorite method for finding perl is the one I use on shebang lines:

#! /usr/bin/env perl

I needed to go to this when I worked in an environment where I wanted to share my scripts but different machines had been set up with different conventions on finding Perl: some in /usr/bin/perl, some in /usr/local/bin/perl, and, on one machine, /var/local/bin/perl.

You haven't explained quite what you're trying to do, but rather than call an external command, it might be simpler to do:

   for (map {"$_/perl"} split /:/, $ENV{PATH}) { if( -x $_) {$binpath=$_; last}}

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.