Variables and Functions

The environment is a collection of information about your system that every program receives. Each entry in the environment consists of a variable name and a string value.

 

Usage

 

You can automatically substitute the text for the variable name in any command. To create the substitution, include a percent sign % and the variable name on the command line or in an alias or batch file, e.g., %comspec. If the name of the variable whose value you want to use is an expression, you can enclose the expression in brackets, e.g., %[%n]. If you want to use a variable or expression for an array variable name, the syntax is %[var[n]].

 

You can create, alter, view, and delete environment variables with the SET, ESET, and UNSET commands.

 

A few environment variables have special meanings for TCC (they are listed in System Variables ).

 

TCC also supports two special types of variables:

 

OnestepInternal variables are similar to environment variables, but are interpreted internally by TCC, and are not visible in the environment. They provide information about your system for use in batch files and aliases. Some of them provide access to information that may change even during the execution of a single command or batch file.

 

OnestepVariable functions are referenced like environment variables, but perform additional actions like file handling, string manipulation and arithmetic calculations. In addition to the variable functions that are internal to TCC, you can use the FUNCTION command to create your own. These latter ones are referred to as user defined variable functions or UDFs.

 

You can return the result of a command with %(command). This is the same as using the @EXEC variable function but a little easier to write.

 

%((...)) will evaluate and substitute the numeric expression. For example:

 

echo %((3+5)) is the answer.

 

%[[...]] will evaluate the conditional expression, and return 0 if the exit status is true; 1 if it is not. For example:

 

echo %[[5 == 6]]

 

You can return the string result of a command with %{command}.  This is the same as @EXECSTR[command] but a little easier to write. For example:

 

dir %{echo foo}

 

will be translated to "dir foo".

 

Note: TCC inherits its initial environment from the process which started it. That process might be Explorer or another existing Windows process which launched the current TCC session. Note that if the starting process's environment is changed (through registry modifications, for example)  while TCC is already running, those changes will not be automatically reflected in TCC's current environment. See the SET command for details.

 

You use the SET command to create a new environment variable. SET can also modify or delete a single environment variable, or display the value of one or more environment variables. ESET allows you to edit an environment variable. UNSET deletes environment variables. For example, you can create a variable named BACKUP like this:

 

set BACKUP=*.bak;*.bk

 

If you then type:

 

del %BACKUP

 

it is equivalent to having type the command:

 

del *.bak;*.bk

 

The environment variable names you use this way may contain any alphabetic or numeric characters, the underscore character _, and the dollar sign $. You can force acceptance of other characters by including the full variable name in square brackets, like this: %[AB##2]. You can also indirectly reference environment variables using square brackets. For example %[%var1] means "the contents of the variable whose name is stored in VAR1".

 

In addition, TCC uses the environment to keep track of the default directory on each drive. Windows only tracks the default directory of the current drive; TCC overcomes this limitation by saving the default directory for each drive in the environment, using hidden variable names. Each variable begins with an equal sign followed by the drive letter and a colon (for example, =C:). You cannot view or change these variables with the SET command.

 

The trailing percent sign that was traditionally required for environment variable names is not usually required by TCC, which accept any character that cannot be part of a variable name as the terminator. However, the trailing percent can be used to maintain compatibility with CMD.

 

The trailing percent sign is needed if you want to append variable values. The following examples show the possible interactions between variables and literal strings. First, create two environment variables called ONE and TWO this way:

 

set ONE=abcd

set TWO=efgh

 

Now the following combinations produce the output text shown:

 

original

expanded

method

%ONE%TWO

abcdTWO

("%ONE%" + "TWO")

%ONE%TWO%

abcdTWO

("%ONE%" + "TWO%")

%ONE%%TWO

abcdefgh

("%ONE%" + "%TWO")

%ONE%%TWO%

abcdefgh

("%ONE%" + "%TWO%")

%ONE%[TWO]

abcd[TWO]

("%ONE%" + "[TWO]")

%ONE%[TWO]%

abcd[TWO]

("%ONE%" + "[TWO]%")

%[ONE]%TWO

abcdefgh

("%[ONE]" + "%TWO")

%[ONE]%TWO%

abcdefgh

("%[ONE]" + "%TWO%")

 

If you want to pass a percent sign to a command, or a string which includes a percent sign, you must use two percent signs in a row. Otherwise, the single percent sign will be seen as the beginning of a variable name and will not be passed on to the command. For example, to display the string "We're with you 100%" you would use the command:

 

echo We're with you 100%%

 

You can also use back quotes around the text, rather than a double percent sign. See Parameter Quoting for details.

 

Environment variables may contain alias names. TCC will substitute the variable value for the name, then check for any alias name which may have been included within the value. For example, the following commands would generate a 2-column directory of the .TXT files:

 

alias d2 dir /2

set cmd=d2

%cmd *.txt

 

For compatibility with some peculiar syntax introduced in recent CMD versions, TCC supports:

 

%var:string1=string2%

Substitutes the second string for all instances of the first string in the variable.

%var:~x[,y]%

Returns the substring starting at the xth character position (base 0) and continuing for y characters. If y is not specified, returns the remainder of the string. If x is negative, starts from the end of the string.

 

For string manipulations, we suggest you rely instead on the much more flexible Variable Functions.