Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

pwd=`pwd`

16 views
Skip to first unread message

James

unread,
May 31, 2017, 7:44:26 PM5/31/17
to
Suppose in a script,
pwd=`pwd`

Does this run once and set the value of $pwd throughout the script
or can this be different value depending on which directory you are in?

TIA
James

Janis Papanagnou

unread,
May 31, 2017, 8:04:37 PM5/31/17
to
Initially you get the working directory where you started the script,
within the script you'll get different directories if you change your
directory in the script. If in doubt just try it

pwd=`pwd` ; echo $pwd ; cd .. ; pwd=`pwd` ; echo $pwd

The variable value $pwd will not change unless you assign a new value,
though. It might be clearer if you'd use the new standard syntax for
that

pwd=$( pwd )

which shows more obviously that you are calling a command in a subshell
(or subshell-like environment), and the value at the time of the call
is assigned. For illustration try this

while [[ $pwd != '/' ]]
do
pwd=$( pwd ) ; echo $pwd ; cd ..
done


Janis

>
> TIA
> James
>

Ed Morton

unread,
May 31, 2017, 8:49:51 PM5/31/17
to
Janis explained the mechanics well, I'm just not sure if it was clear that

pwd=`pwd`

contains 2 different entities which unfortunately have both been named "pwd":

1) A variable named "pwd"
2) The UNIX tool named "pwd"

We can't change the tool name "pwd" so lets change the variable name "pwd" to
"thisdir" so your command line is then:

thisdir=`pwd`

Hopefully that makes it clear that what you are doing is simply setting a
variable - you are not affecting future results of calling the tool "pwd" and
that variable "thisdir" will have the value returned by THAT call to "pwd" until
you specifically set it to some other value.

Ed.

P.S. And yes you should be using $(pwd) instead of `pwd`.
0 new messages