Version 4 of cd

Updated 2003-03-17 10:42:12

http://purl.org/tcl/home/man/tcl8.4/TclCmd/cd.htm


A frequently made mistake FMM [L1 ] that programmers make is to attempt to exec the cd command. cd stands for change directory. This sets a kernel specific piece of information regarding what files within a filesystem the application will see if they use relative pathnames (that is to say, pathnames either beginning with ./ , ../ , or without any leading directory indicatory).

This kernel value is a unique value per process - changing it in one process won't change it in other existing processes.


Stacking cd: The following overloaded version keeps a stack of visited directories (implicit "pushd"). The "popd" functionality fires if called as "cd -":

 if {[info command tcl::cd]==""} {rename cd tcl::cd}
 proc cd {{dir ""}} {
    global cd_stack
    if {$dir=="-"} {
        set dir [lindex $cd_stack end]
        set cd_stack [lrange $cd_stack 0 end-1]
    } else {
       if {$dir==""} {set dir $::env(HOME)}
       lappend cd_stack [pwd]
    }
    tcl::cd $dir
 } ;# RS

Tcl syntax help - Arts and crafts of Tcl-Tk programming - Category Command