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

2019-07-27 04:30发布

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 -

标签: bash cd
1条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-07-27 05:07

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 -
查看更多
登录 后发表回答