PATH

The PATH environment variable is used by the OS when exec calls are made. Tcl invokes exec during both the exec command, as well as the open pipe style call.

If you specify a full pathname for the command in either of these cases, exec directly invokes the command. Otherwise, the system call works as documented for your system, which is typically to walk through the path, a directory at a time, reading through the directory until a suitable (defined by the OS) file is located for execution.


LV example of some code to read through $PATH, looking for all the places a command might be found:

 set cmd $::argv
 set sep ";"            ; # For windows. 
 set sep ":"            ; # For unix. 
 set dirlst [split $::env(PATH) ":"]
 foreach dir $dirlst {
  set tstfile [file join $dir $cmd]
  if {[file exists $tstfile]} {
        puts "$tstfile exists"
  } 
 }

I need to test the above to see if it works with files that contain special characters, like space.

Yikes - there's a bug! The path I get on Windows (in MKS toolkit korn shell) has drive designators on each of the path designators and uses ";" instead of ":" as directory separators.


D. McC: This works for me to find an executable in $PATH on Linux:

 proc inpath {prog} {
        set exok 0
        # Original Linux version of "pathlist":
        set pathlist [split $::env(PATH) ":"]
        # Modified non-Linux version of "pathlist"--delete this line on Linux:
        set pathlist [split $::env(PATH) \
                [expr {$::tcl_platform(platform) == "windows" ? ";" : ":"}]]
        foreach dir $pathlist {
                if {[file executable [file join $dir $prog]]} {
                        set exok 1
                        break
                }
        }
        return $exok
 }

# Examples:

 % inpath supernotepad
 1
 % inpath bogomips
 0

LV MacOS or Windows may need to change that ":" to another character, depending on what their shell uses for PATH delimiters.

MG On windows, it's always been a semi-colon. I've altered the example above to work on Windows, too (though, when looking for an exec, auto_execok works better). In the code above, you need to use inpath wish.exe, as opposed to auto_execok wish without the extension.