dgillman has asked for the wisdom of the Perl Monks concerning the following question:

I ported a perl script from perl5.10 on Ubuntu10 to perl5.8.8 on Ubuntu8.04.4, and `echo -e ...` stopped working.

Here's the output of a toy script on perl5.8.8 on Ubuntu8.04.4:

-e hi there hi there /bin/echo
Here's the toy script:
#!/usr/bin/perl -w my $result = `echo -e 'hi there'`; print "$result"; $result = `/bin/echo -e 'hi there'`; print "$result"; $result = `which echo`; print "$result";

I solved my current problem by replacing echo with /bin/echo, but what's going on here? (How come `which echo` doesn't tell me what's being used?) In general how do you control what linux command you're using in backticks?

Replies are listed 'Best First'.
Re: which echo
by thenaz (Beadle) on Aug 04, 2011 at 17:57 UTC

    The way "echo" is interpreted depends on your shell. For some shells, it's a built-in, so it doesn't actually invoke /bin/echo (which is apparently what you are experiencing). Invoking /bin/echo explicitly will force the shell to invoke the command, rather than use it's built-in implementation.

Re: which echo
by Anonymous Monk on Aug 04, 2011 at 19:34 UTC
    You want control AND to use backticks? Backticks call out to the shell, so control is gone. The best you can do with backticks is understand how each shell works and possibly how to override which shell your being given by $ENV or /etc/passwd .

    If you want control, use a 3 argument open() w/ MODE set to '-|'.

    TJD

Re: which echo
by cdarke (Prior) on Aug 05, 2011 at 07:19 UTC
    Your toy script works for me. Did you test $? ($CHILD_ERROR)? That might have told you more.

    As for the -e option, SUS 4 (Single UNIX Specification, also known as POSIX) says for echo(1): "Implementations shall not support any options."
Re: which echo
by ohcamacj (Beadle) on Aug 05, 2011 at 03:23 UTC
    Agree with thanaz, it's shell dependent.
    sh -c "echo -e 'hi there'" -e hi there
    On some systems. Seems to be when dash (a simpler, faster shell), is used for sh instead of bash.
    bash -c "echo -e 'hi there'" hi there /bin/echo -e 'hi there' hi there
    But why are you calling echo from perl in the first place? It's not necessary.