Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm using a command line program whose documentation states:

"You can run XXX on input located in another directory, but its output will always be put in the current working directory". I'd like to have the output written to another directory though.

I could (either with cd, or pushd + popd)

  1. change to the output directory
  2. run the command so the output is put there
  3. then change back


  1. run the command in my current location
  2. move the output to the output folder

...but both seem clunky, and prone to errors if the script were to be edited by someone who isn't aware of this requirement. The only other solution I can think of is to join the commands with semicolons. Is there another way?

This is in bash by the way.

share|improve this question
up vote 4 down vote accepted

use parentheses, so the cd occurs in a subshell:

(cd other_dir && xxx)

When that ends, your current working dir will not have changed.

cd can fail, so use && to conditionally execute the program.

share|improve this answer
Works! Just one detail, which is makes sense once I think about it: The arguments passed to XXX behave as if it was called inside other_dir (which it was). – pufferfish Feb 22 '14 at 11:34
Something in the command probably cd's out of that directory - you could perhaps create a symlink of the directory you want it to be run in to replace the directory it runs in... – Wilf Feb 22 '14 at 15:14

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.