PreviousNext

Command Substitution Offers Flexibility and Convenience

Command substitution provides a convenient way to express the return value of one command within another command. This is useful when you want to use the return value of one command as input to another command. Use brackets to invoke command substitution. The following example uses the expr command which we will discuss shortly. Generally, expr performs a math function, returning the computed value expressed by its arguments.

dcecp> set a 4
4
dcecp> set b [expr $a+2]
6
dcecp> set b
6
dcecp>

A more practical example might use command substitution for a command that returns a long name or a list. Let's recall an example we saw in DCE Control Program Introduction . In this example, the [group list temps] command returns a list to the foreach command which performs the account modify operation on each element in the list. We will look more closely at the foreach looping command later in this topic.

dcecp> foreach i [group list users] {
> account modify $i -change {expdate 1995-12-31}}
dcecp>

Another practical use of command substitution is to set up a test condition for an if statement. We show an example of this usage in if Statements Conditionalize Your Scripts .