临时更改当前工作目录在bash运行一个命令 [英] Temporarily change current working directory in bash to run a command

查看:847
本文介绍了临时更改当前工作目录在bash运行一个命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我可以使用 CD 命令来改变我的工作目录在bash。

但是,如果我这样做的命令:

  CD SOME_PATH和放大器;&安培; run_some_command

然后,工作目录将永久改变。是否有某种方式来改变工作目录只是暂时这样?

  PWD = SOME_PATH run_some_command


解决方案

您可以通过内附的命令行中的一对运行 CD 和可执行的子shell括号:

 (CD SOME_PATH&放大器;&安培; exec_some_command)

演示:

  $ PWD
/家/作者Abhijit
$(CD / tmp目录和放大器;&安培; PWD)#目录中的子shell改变
/ tmp目录
$ PWD#父shell的PWD还是一样
/家/作者Abhijit

I know I can use cd command to change my working directory in bash.

But if I do this command:

cd SOME_PATH && run_some_command

Then the working directory will be changed permanently. Is there some way to change the working directory just temporarily like this?

PWD=SOME_PATH run_some_command

解决方案

You can run the cd and the executable in a subshell by enclosing the command line in a pair of parentheses:

(cd SOME_PATH && exec_some_command)

Demo:

$ pwd
/home/abhijit
$ (cd /tmp && pwd)  # directory changed in the subshell
/tmp 
$ pwd               # parent shell's pwd is still the same
/home/abhijit

这篇关于临时更改当前工作目录在bash运行一个命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆