Getting current path in variable and using it

2020-02-08 10:20发布

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?

标签: linux bash
5条回答
等我变得足够好
2楼-- · 2020-02-08 10:46

Something like this should work:

myvar=`pwd`
# ...
cd $myvar
查看更多
乱世女痞
3楼-- · 2020-02-08 10:47
myvar="$PWD"
cd "$myvar"

(Quotes are necessary if your path contains whitespaces.)

查看更多
放荡不羁爱自由
4楼-- · 2020-02-08 10:48

in bash

$ a=$(pwd)
查看更多
Emotional °昔
5楼-- · 2020-02-08 10:56

Ind addition to the pwd command and the $PWD environment variable, I'd also suggest you look into pushd/popd:

/$ pushd /usr
/usr /
/usr$ pushd /var/log
/var/log /usr /
/var/log$ popd
/usr /
/usr$ popd
/
/$
查看更多
家丑人穷心不美
6楼-- · 2020-02-08 10:58

It worked for me:

currentdir=$(cd -)
printf "Generating content at $currentdir\n"
查看更多
登录 后发表回答