Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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

or

  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
add comment

1 Answer 1

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 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 at 15:14
add comment

Your Answer

 
discard

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.