Search code examples
perlshbackticks

Perl backticks when using pipes


I'm experiencing some problems trying to capture the output of a simple command:

$timeTotal = `echo $timeTotal + $time | bc -l`;

But I'm getting the following errors:

sh: +: not found
sh: Syntax error: "|" unexpected

This command works perfectly in bash but it seems sh is being actually used. At the very beginning I thought that the problem is the pipe usage (although the sum is not well interpreted neither). What confuses me is that the following command in the same script causes no error and works properly:

my $time = `cat $out.$step | bc -l`;

Any suggestions?


Solution

  • $timeTotal contains a trailing newline it shouldn't, so you're executing

    echo XXX
    

    and

    + YYY | bc -l
    

    instead of

    echo XXX + YYY | bc -l
    

    You're surely missing a chomp somewhere.

    There's also a double-quote in your command that's out of place.