Skip to content
Advertisement

How can I execute a command inside another folder and return to original directory?

I use this command to make inside a folder:

cd build/ && make 

But I want to return to original directory after the command

I tried this:

cd build/ && make && cd ..

But this doesn’t work. How can I do it? Would help in quick testing…

Advertisement

Answer

Use a subshell, like so:

$ pwd
/tmp

$ (cd build && make)

$ pwd
/tmp

The shell forks before running the commands in brackets, so once it completes it’s as if you never left the current directory.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement