Go to main content

man pages section 1: User Commands

Exit Print View

Updated: Wednesday, February 9, 2022

eval (1t)


eval - Evaluate a Tcl script


eval arg ?arg ...?


eval(1t)                     Tcl Built-In Commands                    eval(1t)


       eval - Evaluate a Tcl script

       eval arg ?arg ...?

       Eval  takes one or more arguments, which together comprise a Tcl script
       containing one or more commands.  Eval concatenates all  its  arguments
       in  the  same  fashion  as  the concat command, passes the concatenated
       string to the Tcl interpreter recursively, and returns  the  result  of
       that  evaluation  (or  any  error generated by it).  Note that the list
       command quotes sequences of words in such a way that they are not  fur-
       ther expanded by the eval command.

       Often,  it  is useful to store a fragment of a script in a variable and
       execute it later on with extra values appended. This technique is  used
       in a number of places throughout the Tcl core (e.g. in fcopy, lsort and
       trace command callbacks). This example shows how to do this using  core
       Tcl commands:

              set script {
                  puts "logging now"
                  lappend $myCurrentLogVar
              set myCurrentLogVar log1
              # Set up a switch of logging variable part way through!
              after 20000 set myCurrentLogVar log2

              for {set i 0} {$i<10} {incr i} {
                  # Introduce a random delay
                  after [expr {int(5000 * rand())}]
                  update    ;# Check for the asynch log switch
                  eval $script $i [clock clicks]

       Note  that  in the most common case (where the script fragment is actu-
       ally just a list of words forming a command prefix), it  is  better  to
       use  {*}$script when doing this sort of invocation pattern.  It is less
       general than the eval command, and hence easier to make robust in prac-
       tice.   The  following procedure acts in a way that is analogous to the
       lappend command, except it inserts the argument values at the start  of
       the list in the variable:

              proc lprepend {varName args} {
                  upvar 1 $varName var
                  # Ensure that the variable exists and contains a list
                  lappend var
                  # Now we insert all the arguments in one go
                  set var [eval [list linsert $var 0] $args]

       However, the last line would now normally be written without eval, like

              set var [linsert $var 0 {*}$args]

       See attributes(7) for descriptions of the following attributes:

       |Availability   | runtime/tcl-8    |
       |Stability      | Uncommitted      |

       catch(n), concat(n), error(n), errorCode(n),  errorInfo(n),  interp(n),
       list(n), namespace(n), subst(n), uplevel(n)

       concatenate, evaluate, script

       Source  code  for open source software components in Oracle Solaris can
       be found at https://www.oracle.com/downloads/opensource/solaris-source-

       This     software     was    built    from    source    available    at
       https://github.com/oracle/solaris-userland.   The  original   community
       source was downloaded from  http://prdownloads.sourceforge.net/tcl/tcl-

       Further information about this software can be found on the open source
       community website at https://www.tcl.tk/.

Tcl                                                                   eval(1t)