21.1.5 Variables
(Ask a Question)With Tcl scripting, you can store a value in a variable for later use. You use the set command to assign variables. For example, the following set command creates a variable named x and sets its initial value to 10.
set x 10
A variable can be a letter, a digit, an underscore, or any combination of letters, digits, and underscore characters. All variable values are stored as strings.
In the Tcl language, you do not declare variables or their types. Any variable can
hold any value. Use the dollar sign ($
) to obtain the value of a
variable, for example:
set a 1
set b $a
set cmd expr
set x 11
$cmd $x*$x
The dollar sign $
tells Tcl to handle the letters and digits
following it as a variable name and to substitute the variable name with its value.
Global Variables
Variables can be declared global in scope using the Tcl global command. All procedures, including the declaration can access and modify global variables, for example:
global myvar