Using a Linux shell, how do I start a program with a different working directory from the current working directory?
For example, I have a binary file helloworld
that creates the file hello-world.txt
in the current directory. This file is inside of directory /a
. Currently I am in directory /b
. I want to start my program running ../a/helloworld
and get the hello-world.txt
somewhere in a third directory /c
.
from the current directory provide the full path to the script directory to execute the command
Just change the last "&&" into ";" and it will cd back no matter if the command fails or succeeds:
If you always want it to go to /C, use an absolute path when you write the file.
An option which doesn't require a subshell and is built in to bash
Demo:
why not keep it simple
cd SOME_PATH && run_some_command && cd -
the last 'cd' command will take you back to the last pwd directory. This should work on all *nix systems.
If you want to perform this inside your program then I would do something like: