Temporarily change current working directory in bash to run a command

158,050

Solution 1

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

Solution 2

bash has a builtin

pushd SOME_PATH
run_stuff
...
...
popd 

Solution 3

Something like this should work:

sh -c 'cd /tmp && exec pwd'
Share:
158,050

Related videos on Youtube

Ethan Zhang
Author by

Ethan Zhang

I am the bone of my JavaScript Linux is my body, and Vim is my blood.

Updated on June 18, 2020

Comments

  • Ethan Zhang
    Ethan Zhang about 4 years

    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
    
    • Sahil
      Sahil over 9 years
      why not keep it simple cd SOME_PATH && run_some_command && cd - the last command will take you back to the last pwd directory.
    • Eyal
      Eyal about 6 years
      @Sahil then it can't be run in parallel
  • tripleee
    tripleee about 12 years
    That sort of invalidates the point of using exec, don't you think?
  • codaddict
    codaddict about 12 years
    @tripleee: I guess OP meant to execute any executable and not the exec.
  • Fr0sT
    Fr0sT about 11 years
    +1, pushd/popd is ideal for this. Just don't forget to popd before you exit.
  • ron rothman
    ron rothman almost 11 years
    Not necessarily a good solution if run_stuff can fail (and the script exits). You'd be stuck in SOME_PATH.
  • Allan Ruin
    Allan Ruin almost 8 years
    not working in shell file
  • galois
    galois over 6 years
    @ron.rothmanℝℝ couldn't you just do something like pushd PATH; (run_stuff); [[ "$?" != 0 ]] && popd; ...; popd
  • jez
    jez over 5 years
    @BeC use function rather than alias. Better two years late than never.
  • octoquad
    octoquad over 4 years
    In my case, if the script execution failed e.g. git pull, I wanted to investigate in the directory with git log.
  • Jason R Stevens CFA
    Jason R Stevens CFA over 4 years
    What an excellent way to quickly update targets under another project while testing. Thanks!
  • mjs
    mjs over 4 years
    best answer. works with multiline wrappers as well and premature exits
  • Damian
    Damian about 4 years
    best answer for me!
  • Hexworks
    Hexworks over 2 years
    I won't see the output of the command if it runs in a subshell.
  • Hexworks
    Hexworks over 2 years
    This should be the accepted answer! A subshell breaks the log output!
  • Pierre
    Pierre over 2 years
    worth noting: if your subshell to run the commands (in parentheses now) and that code does an "exit" (you want to stop execution), control goes back to the parent shell (the parent shell does NOT exit). This might be a difference if you refactor your code (execution doesn't stop anymore if something bad happens in the parentheses)