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.
Roberto Aloi
2010-03-09 11:08:47
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
2010-03-09 11:16:42
Strictly speaking you don't "return" to the current directory since you never left it in the scope of the parent shell.
Sorpigal
2010-03-09 11:17:11
@Ignacio: Yep, but for what I need is fine. @Sorpigal: Good point. I'm aware of that :)
Roberto Aloi
2010-03-09 11:20:19
+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
2010-03-09 11:10:29
+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
2010-03-09 11:11:50
Accepting. Even if I'm starting a couple of sub-shells in my own script, this sounds as the best alternative.
Roberto Aloi
2010-03-09 11:28:39