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

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

问题描述

我知道我可以使用 cd 命令在 bash 中更改我的工作目录.

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

推荐答案

通过将命令行括在一对括号中,您可以在子 shell 中运行 cd 和可执行文件:

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)

演示:

$ 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天全站免登陆