views:

41

answers:

3

I'm a complete newbie to bash scripting. I remember there was a way to execute the cd command, automatically returning to the current directory (without an explicit cd ..). Any idea?

+1  A: 

Found! I can execute it as a sub-shell.

A command list embedded between parentheses runs as a subshell.

SOURCE: http://tldp.org/LDP/abs/html/subshells.html

Roberto Aloi
Do note that running it in a subshell means that things like setting environment variables to be used outside won't work.
Ignacio Vazquez-Abrams
Strictly speaking you don't "return" to the current directory since you never left it in the scope of the parent shell.
Sorpigal
@Ignacio: Yep, but for what I need is fine. @Sorpigal: Good point. I'm aware of that :)
Roberto Aloi
+1  A: 

Also, pushd and popd can come in very handy - they represent operations on a stack of directory locations - allowing you to "travel back in time".

vstrien
+2  A: 

If you just want to go back to the last directory, you can use cd -.

If you need more places to go back to, try pushd <dir> (instead of cd <dir>) and then you can go back with popd.

Aaron Digulla
Good to know. Thanks :)
Roberto Aloi
Accepting. Even if I'm starting a couple of sub-shells in my own script, this sounds as the best alternative.
Roberto Aloi