How to find out the path that will be used in `cd -`?

断了今生、忘了曾经 提交于 2019-12-11 08:44:48

问题


I know that it is possible to use cd - to switch between 2 paths:

user@server:~$ cd a
user@server:~/a$ cd ~/b
user@server:~/b$ cd -
/home/user/a
user@server:~/a$ cd -
/home/user/b
user@server:~/b$

I want to use this feature to do something with the previous path. Maybe there is some variable that points to the previous path so I can do thins like:

user@server:~/a$ cd ~/b
user@server:~/a$ ls -d $PREVIOUS_PATH
/home/user/a
user@server:~/a$ cp file $PREVIOUS_PATH # will copy file to /home/user/a
user@server:~/b$ cd -

回答1:


Old working directory is stored in OLDPWD environment variable. This variable is updated every time we change directory. This also means that it is not set when we launch terminal.

user@server:~/a$ cd ~/b
user@server:~/a$ ls -d "$OLDPWD"
/home/user/a
user@server:~/a$ cp file "$OLDPWD" # will copy file to /home/user/a, ""
user@server:~/b$ cd -


来源:https://stackoverflow.com/questions/45115998/how-to-find-out-the-path-that-will-be-used-in-cd

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!