On Unix, cd -
can switch back to previous directory. I was wondering if there's a similar command in tcl interpreter can do the same?
Asked
Active
Viewed 1,406 times
2 Answers
4
Tcl doesn't maintain a directory history/stack by default; you'd have to add one. You can do that in pure Tcl though:
rename cd real_cd
variable old_dir [pwd]
proc cd {directory} {
variable old_dir
if {$directory eq "-"} {
set directory $old_dir
}
set old_dir [pwd]
real_cd $directory
}
This is a very simple version that only remembers the last location, but everything else can be built on top in the same way.

Donal Fellows
- 133,037
- 18
- 149
- 215
3
No, there is no directory stack in Tcl, and previous directory is not remembered anywhere.
TclX provides pushd
and popd
commands for directory stack, but it does not modify cd
command to put anything there. It can be done by command wrapping and renaming, but I wouldn't recommend it for something that can be used in a script (at least, don't override semantics of cd -
).

Anton Kovalenko
- 20,999
- 2
- 37
- 69