Version 8 of cd

Updated 2005-11-03 04:04:32

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.

AMG: Here's an exception. exec'ing cd is actually correct when the "cd" in question comes from the execline package [L2 ], in which everything is accomplished through Bernstein chaining. See [L3 ] for documentation on this variant of cd.


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

Variant with a little more error checking, an no reliance on env(HOME):

 if {[llength [info command tcl::cd]] == 0} { rename cd tcl::cd }
 proc cd {{dir {}}} {
     variable tcl::cd_lastdir
     set pwd [pwd]
     if {$dir eq "-"} {
         if {![info exists cd_lastdir]} { return }
         set dir $cd_lastdir
     } elseif {[llength [info level 0]] == 1} {
         # no $dir specified - go home
         set code [catch {tcl::cd } res]
     }
     if {![info exists code]} {
         set code [catch {tcl::cd $dir} res]
     }
     if {!$code} { set cd_lastdir $pwd }
     return -code $code $res
 }; # JH

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