$current_path=`echo $cwd` does not work

K

Kuhl

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 .
#
 
J

Jürgen Exner

Kuhl said:
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
 
R

Ron Bergin

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`;
 
J

John Bokma

Kuhl said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top