[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

13.2.4.3 Dereferencing Variables

Dereferencing a variable means accessing its value. The simplest form of dereferencing is by prepending a dollar sign to the variable name:

 
foo = 1
print foo => foo
print $foo => 1

Notice, that in the example above, the first print statement understands foo as a literal string, whereas the second one prints the value of the variable.

Dereferencing an undefined variable produces error message:

 
print $x error--> variable `x' used before definition

Optionally, the variable name may be surrounded by curly braces. Both $foo and ${foo} are equivalent. The use of the latter form is obligatory only when the variable name coincides with one of the reserved keywords (see section 13.2.3 Reserved Keywords). It also can be used to resolve ambiguity between using dash as a part of user name and as a subtraction operator:

 
long-name = 2
$long-name => 2
$long-name-1 error--> variable `long-name-1' used before definition
${long-name}-1 => 1
$long-name - 1 => 1

We recommend to always surround `-' with whitespace when it is used as arithmetic operator.

The ${} notation also permits some operations similar to shell variable substitution.

${variable:-text}
Use default values. If variable is unset, return text, otherwise return the value of the variable.

 
$x error--> variable `x' used before definition
${x:-1} => 1
x = 2
${x:-1} => 2

${variable:=text}
Assign default values. If variable is unset, text is assigned to it. The expression always returns the value of the variable.

 
$x error--> variable `x' used before definition
${x:=1} => 1
$x => 1

${variable:?text}
Display error if unset. If variable is unset, text is written to the standard error (if text is empty, the default diagnostic message is used) and further execution of the program is aborted. Otherwise, the value of variable is returned.

 
$x error--> variable `x' used before definition
${x:?} error--> x: variable unset
${x:?foobar} error--> foobar

${variable::text}
Prompt for the value if unset. If variable is unset, radtest prints text (or a default message, if it is empty), reads the standard input up to the newline character and returns the value read. Otherwise, the value of the variable is returned. This notation provides a convenient way for asking user to supply default values.

 
${x::} -| (<teletype>:1)x?
${x::Enter value of x: } -| Enter value of x: 

${variable:&text}
Prompt for the value with echo turned off if unset. This is similar to the ${variable::text}, with the exception that the input value will not be echoed on the screen. This notation provides a convenient way for asking user to supply default values for variables (such as passwords, shared secrets, etc.) while preventing them from being compromised.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

This document was generated by Sergey Poznyakoff on November, 20 2004 using texi2html