bash 如何在bash中快速返回上一个工作目录?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10382299/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 02:09:34  来源:igfitidea点击:

How to return to previous working directory quickly in bash?

bashshell

提问by Ethan Zhang

When I change into a directory with the cdcommand, I lose the previous working directory, unless I remember it in my memory. Is there some handy method to go back quickly?

当我用cd命令切换到一个目录时,我丢失了以前的工作目录,除非我记得它。有什么方便的方法可以快速返回吗?

Demo:

演示:

$ cd ~/some_path
$ cd /another_path
$ command_to_go_back_to_some_path

采纳答案by pizza

You can also do this

你也可以这样做

$ pushd ~/some_path
$ pushd /another_path
$ popd 
$ popd

回答by Darragh Enright

You can go back to the last dir with cd -

你可以回到最后一个目录 cd -

回答by codaddict

As mentioned you can use cd -. The shell internally does a cd $OLDPWD.

如前所述,您可以使用cd -. shell 在内部做了一个cd $OLDPWD.

回答by yantaq

if you want to use it in script and suppress the output do this:

如果您想在脚本中使用它并抑制输出,请执行以下操作:

cd - > /dev/null

回答by Benjamin W.

For usage in a script, you could use the OLDPWDshell variable: it contains the previous working directory.

对于在脚本中的使用,您可以使用OLDPWDshell 变量:它包含以前的工作目录。

$ pwd
/home/username
$ cd /usr/bin
$ pwd
/usr/bin
$ cd "$OLDPWD"
$ pwd
/home/username

I prefer this over cd -in scripts because I don't have to suppress any output.

我更喜欢cd -在脚本中使用它,因为我不必抑制任何输出。