Temporarily change current working directory in bash to run a command [duplicate]

大兔子大兔子 提交于 2019-11-27 17:03:50
codaddict

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 has a builtin

pushd SOME_PATH
run_stuff
...
...
popd 

Something like this should work:

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