$current_path=`echo $cwd` does not work

Discussion in 'Perl Misc' started by Kuhl, Sep 27, 2008.

  1. Kuhl

    Kuhl Guest

    Hi, all:

    Backticks `` are supposed to be able to transfer the result of shell
    commands to Perl variable.
    But it's very confusing that it does not behave in the way that I
    think it should do.

    $current_path=`pwd`; This works.
    $current_path=`echo $cwd`; This does not work.

    But in fact, pwd is aliased to echo $cwd. Details see below.

    What's the issue? How to fix it?

    Thanks.


    # which pwd
    pwd: aliased to echo $cwd
    # pwd
    /home/user/shell
    # echo $cwd
    /home/user/shell

    #!/usr/bin/perl
    $current_path=`pwd`;
    print "\nCurrent path is $current_path .\n";
    $current_path=`echo $cwd`;
    print "\nCurrent path is $current_path .\n";
    $current_path=`echo \$cwd`;
    print "\nCurrent path is $current_path .\n";
    $current_path=`"echo $cwd"`;
    print "\nCurrent path is $current_path .\n";
    exit 0;


    But the result of this script is:

    Current path is /home/user/shell
    ..

    Current path is
    ..

    Current path is
    ..
    sh: line 1: echo : command not found

    Current path is .
    #
     
    Kuhl, Sep 27, 2008
    #1
    1. Advertising

  2. Kuhl <> wrote:
    >Hi, all:
    >
    >Backticks `` are supposed to be able to transfer the result of shell
    >commands to Perl variable.
    >But it's very confusing that it does not behave in the way that I
    >think it should do.
    >
    >$current_path=`pwd`; This works.
    >$current_path=`echo $cwd`; This does not work.
    >
    >But in fact, pwd is aliased to echo $cwd. Details see below.


    A guess: those aliases are not valid in your _system_ shell but only in
    the shell that you are using as your standard shell from the command
    line?

    Try calling print `alias` to find out, if those aliases are even
    defined.

    jue
     
    Jürgen Exner, Sep 27, 2008
    #2
    1. Advertising

  3. Kuhl

    Ron Bergin Guest

    On Sep 27, 7:10 am, Kuhl <> wrote:
    > Hi, all:
    >
    > Backticks `` are supposed to be able to transfer the result of shell
    > commands to Perl variable.
    > But it's very confusing that it does not behave in the way that I
    > think it should do.
    >
    > $current_path=`pwd`; This works.
    > $current_path=`echo $cwd`; This does not work.
    >

    $cwd is being seen as a Perl scalar that is interpolated by Perl
    before passing it to the shell...you need to escape it.

    $current_path=`echo \$cwd`;
     
    Ron Bergin, Sep 27, 2008
    #3
  4. Kuhl

    John Bokma Guest

    Kuhl <> wrote:

    > Hi, all:
    >
    > Backticks `` are supposed to be able to transfer the result of shell
    > commands to Perl variable.
    > But it's very confusing that it does not behave in the way that I
    > think it should do.
    >
    > $current_path=`pwd`; This works.
    > $current_path=`echo $cwd`; This does not work.


    [..]
    > #!/usr/bin/perl


    use strict;
    use warnings;

    and all becomes clear. It does work, you only misunderstand ``.

    There is a very good reason why those two lines are recommended daily in
    this group.

    --
    John http://johnbokma.com/ - Hacking & Hiking in Mexico

    Perl help in exchange for a gift:
    http://johnbokma.com/perl/help-in-exchange-for-a-gift.html
     
    John Bokma, Sep 27, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Burganovsky
    Replies:
    1
    Views:
    465
    Mark Rae
    Dec 15, 2005
  2. Burganovsy
    Replies:
    0
    Views:
    459
    Burganovsy
    Dec 11, 2005
  3. noisenet
    Replies:
    0
    Views:
    417
    noisenet
    May 29, 2007
  4. Brock Rycenga

    Rake "sh" does not echo command

    Brock Rycenga, Oct 22, 2008, in forum: Ruby
    Replies:
    2
    Views:
    126
    Brock Rycenga
    Oct 22, 2008
  5. Kuhl
    Replies:
    1
    Views:
    298
    Tad J McClellan
    Dec 22, 2008
Loading...

Share This Page