programing

명령을 실행하도록 bash의 현재 작업 디렉토리 일시 변경

instargram 2023. 5. 17. 22:27
반응형

명령을 실행하도록 bash의 현재 작업 디렉토리 일시 변경

사용할 수 있습니다.cdbash에서 내 작업 디렉토리를 변경하는 명령.

하지만 내가 이 명령을 수행하면:

cd SOME_PATH && run_some_command

그러면 작업 디렉토리가 영구적으로 변경됩니다.이렇게 일시적으로 작업 디렉토리를 변경할 수 있는 방법이 있습니까?

PWD=SOME_PATH run_some_command

실행할 수 있습니다.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

bash에 내장되어 있습니다.

pushd SOME_PATH
run_stuff
...
...
popd 

다음과 같은 방법이 사용될 것입니다.

sh -c 'cd /tmp && exec pwd'

언급URL : https://stackoverflow.com/questions/10382141/temporarily-change-current-working-directory-in-bash-to-run-a-command

반응형