Getting current path in variable and using it


Question

I would like to extract the current path in a variable and use it later on in the script

Something like:

myvar = pwd

Later on:

cd myvar

But my bash skills have rusted over the years.

How would i go on about doing that?

1
28
10/28/2009 10:23:47 AM

Accepted Answer

myvar="$PWD"
cd "$myvar"

(Quotes are necessary if your path contains whitespaces.)

58
10/28/2009 10:25:55 AM

Something like this should work:

myvar=`pwd`
# ...
cd $myvar

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon