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

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

This question is related to bash shell terminal working-directory

The answer is


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

Something like this should work:

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

bash has a builtin

pushd SOME_PATH
run_stuff
...
...
popd 

Examples related to bash

Comparing a variable with a string python not working when redirecting from bash script Zipping a file in bash fails How do I prevent Conda from activating the base environment by default? Get first line of a shell command's output Fixing a systemd service 203/EXEC failure (no such file or directory) /bin/sh: apt-get: not found VSCode Change Default Terminal Run bash command on jenkins pipeline How to check if the docker engine and a docker container are running? How to switch Python versions in Terminal?

Examples related to shell

Comparing a variable with a string python not working when redirecting from bash script Get first line of a shell command's output How to run shell script file using nodejs? Run bash command on jenkins pipeline Way to create multiline comments in Bash? How to do multiline shell script in Ansible How to check if a file exists in a shell script How to check if an environment variable exists and get its value? Curl to return http status code along with the response docker entrypoint running bash script gets "permission denied"

Examples related to terminal

Git is not working after macOS Update (xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools) Can't compile C program on a Mac after upgrade to Mojave Flutter command not found VSCode Change Default Terminal How to switch Python versions in Terminal? How to open the terminal in Atom? Color theme for VS Code integrated terminal How to edit a text file in my terminal How to open google chrome from terminal? Switch between python 2.7 and python 3.5 on Mac OS X

Examples related to working-directory

How to get current relative directory of your Makefile? How to get the current working directory using python 3? Get current batchfile directory R command for setting working directory to source file location in Rstudio How can Bash execute a command in a different directory context? Temporarily change current working directory in bash to run a command Shell script current directory? How to get current working directory in Java? how to get the current working directory's absolute path from irb How to set the current working directory?