Accessing Environment Variables - 2021.2 English

Vivado Design Suite User Guide: Using Tcl Scripting (UG894)

Document ID
UG894
Release Date
2021-11-17
Version
2021.2 English

Tcl provides a convenient way to access the environment variables in a read-only mode through the Tcl global variable env. The variable env is a Tcl array that is automatically created and initialized at startup inside the Tcl interpreter.

Note: After initialization, any change to the env variable is not applied to the environment outside of the Tcl interpreter. Similarly, any change to your environment variables done after starting the Tcl interpreter will not be reflected by the env variable.

The keys of the env array are the environment variables at the time Vivado Design Suite starts. The keys are case sensitive.

For example:

Vivado% puts "The PATH variable is $env(PATH) "

To get the list of all the Unix environment variables:

Vivado%: set all_env_var [array names env]

It is possible to check if an environment variable exists (i.e a key to env array exists) by using the info command. For example to check for MYVARNAME:

Vivado% if {[info exists env(MYVARNAME)]} { … }

The env array is a global variable and can therefore be referenced inside a proc after being declared as global.

For example:

proc print_env {} {
  global env
  puts " UNIX Environment:"
  foreach var [lsort [array names env]] {
    puts "  $var : $env($var)"
  }
}