Explicitly setting the current directory (pipeline shell)

282 views Asked by At

SHORT: How do I explicitly set the current working directory?

LONG: So I have 52 programs daisy chained together. I have a shell script pipeline that works great. Only problem is I can only run it if I cd into the directory with the files and run it. Some of the sub-programs do not have a mechanism that allows me to explicitly set output directories. They dump everything into the current working directory. This is fine if you are running 1 instance of this pipeline, but not so great if you are trying to process a dozen data-sets one after another. I know I can get the current working directory with:

echo $PWD

But how do I set it?

1

There are 1 answers

4
David Grayson On

You can set the current dirctory for individual programs in your pipeline without affecting the other program in your pipeline like this:

PWD=path1 command1 && PWD=path2 command2

In general, you can set any environment variable using that syntax. Here is a real example I tried in bash:

$ PWD=/home ./test.rb && PWD=/ ./test.rb 
Running in /home
Running in /