我知道可以使用
cd
命令在Bash中更改工作目录。但如果我运行以下命令:cd SOME_PATH && run_some_command
那么工作目录将会永久更改。有没有一种临时更改工作目录的方法,就像这样?
PWD=SOME_PATH run_some_command
cd
命令在Bash中更改工作目录。但如果我运行以下命令:cd SOME_PATH && run_some_command
那么工作目录将会永久更改。有没有一种临时更改工作目录的方法,就像这样?
PWD=SOME_PATH run_some_command
通过将命令行放在一对括号中,您可以在子shell中运行cd
和可执行文件:
(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
function
而不是alias
。迟到两年总比永远不来好。 - jezbash有一个内置的功能
pushd SOME_PATH
run_stuff
...
...
popd
pushd PATH; (run_stuff); [[ "$?" != 0 ]] && popd; ...; popd
- galois像这样应该可以工作:
sh -c 'cd /tmp && exec pwd'