Environment Variables

   

Practical Programming in Tcl & Tk, Third Edition
By Brent B. Welch

Table of Contents
Chapter 9.  Working with Files and Programs


Environment variables are a collection of string-valued variables associated with each process. The process's environment variables are available through the global array env. The name of the environment variable is the index, (e.g., env(PATH)), and the array element contains the current value of the environment variable. If assignments are made to env, they result in changes to the corresponding environment variable. Environment variables are inherited by child processes, so programs run with the exec command inherit the environment of the Tcl script. The following example prints the values of environment variables.

Example 9-12 Printing environment variable values.
 proc printenv { args } {    global env    set maxl 0    if {[llength $args] == 0} {       set args [lsort [array names env]]    }    foreach x $args {       if {[string length $x] > $maxl} {          set maxl [string length $x]       }    }    incr maxl 2    foreach x $args {       puts stdout [format "%*s = %s" $maxl $x $env($x)]    } } printenv USER SHELL TERM => USER   = welch SHELL  = /bin/csh TERM   = tx 

Note: Environment variables can be initialized for Macintosh applications by editing a resource of type STR# whose name is Tcl Environment Variables. This resource is part of the tclsh and wish applications. Follow the directions on page 28 for using ResEdit. The format of the resource values is NAME=VALUE.


       
    Top
     



    Practical Programming in Tcl and Tk
    Practical Programming in Tcl and Tk (4th Edition)
    ISBN: 0130385603
    EAN: 2147483647
    Year: 1999
    Pages: 478

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net