Setting Variables and Quoting

You're reading this page if you don't understand some other documentation that says set X to Y or something similar. This page provides a quick overview of setting variables and quoting.

The set command is used to assign values to variables. Set commands look like this:

   set var value

The value part of the command is sometimes quoted but need not be if there is no embedded whitespace (spaces, newlines, tabs, etc). Numbers, in particular, never have whitespace so we don't quote them. Here are some examples:

   set iq 1
   set age 2000.4
   set name Don
   set name "Don"
   set fullname "Don Libes"

The quotes (if present) aren't part of the value. They merely serve to group characters together into a single value.

Certain characters require special handling within a string. The characters are " (quote itself), \ (backslash), $ and [ and ]. Each of these should be prefixed with a backslash to produce the literal character itself. For example:

   set money \$1000
   set backslash \\
   set quote1 "I love potatoes \[sic\]."
   set quote2 "I said \"Enough already!\" - didn't you hear me?"

As before, the outermost quotes are necessary only if the strings have embedded whitespace.

It should now be apparent why you need to quote literal quotes - because otherwise, they would prematurely terminate the string itself. Each of the other characters has similarly special uses.

Backslashes force the next character to be treated specially. As in our previous example, \[ is treated as a literal left bracket. Backslashes also produce special characters. For example \n is a newline and \t is a tab.

$ causes the immediately following variable name to be replaced with its value. The following example sets fullname to the value Don Libes.

   set name1 Don
   set name2 Libes
   set fullname "$name1 $name"

Anything between brackets is consider a command and evaluated. Anything returned by the command is substituted into the string in place of the bracketted command. For example the pid command returns the process id. Thus we can do this:

    set x "The process id is [pid]."

There is only one other type of quoting. Braces are used to quote commands that are to be evaluated at a later time. Braces prevent special handling of any characters so don't add backslashes inside of braced strings (unless the strings need the backslashes even without the braces). Examples:

    set code    {set x "The process id is [pid]."}
    set code2   {set quote1 "I love potatoes \[sic\]."}

One last note - some predefined variables contain characters such as :: or (). These characters have special properties but don't change anything described so far. You still set them and quote their values the same way. For example:

    set sleep(imap) 30

More?

If you're interested in learning more about Tcl, here are some URLs for more information.



Go to tkbiff homepage.
Go to tkbiff documentation.

Last edited: Tue Oct 8 15:49:21 EDT 2002 by Don Libes