6.2.22 Setting a variable value (set command)
The set command sets a value for a shell variable. By specifying an assignment expression in the argument, you can evaluate the expression and then set a value in the variable. There is no abbreviation for the set command. The following shows the format of the set command:
set assignment-expression
The following shows the format of assignment expression:
variable-name={variable-name|numeric-value|"character-string"}
variable-name (left-hand term): Specifies the name of a shell variable. The value of the right-hand term is assigned to the specified variable.
variable-name (right-hand term): Specifies the name of a shell variable. The value of this specified variable is assigned to the variable of the left-hand term.
numeric-value: Specifies an integer value. The specified integer value is assigned to the variable of the left-hand term.
character-string: Specifies a character string. The specified character string is assigned to the variable of the left-hand term.
The following describes the set command's processing.
- When the set command argument is specified
Assignment expression
The command sets a variable value according to the specified assignment expression.
Assignment expression containing a variable name that has not been created
The command outputs an error message.
Other than an assignment expression
The command outputs an error message.
- When the set command argument is omitted
The command outputs an error message.
- Notes:
If the job definition script is not being run by the run command, an error results because variable information has not been specified.
If you specify a character string, you must enclose it in double quotation marks ("). To use a double quotation mark as part of the character string, specify it as a backslash followed by a double quotation mark (\"). To specify \", specify \\\".
The command processes the argument following the first equal sign (=) in the assignment expression as a variable name and a numeric value or a character string.
If you specify an array for the variable name, specify an array element.
Do not attach a dollar sign ($) to a variable name.
If a specified numeric value is outside the permitted value range for the signed long type, it is rounded to the maximum or minimum value permitted for the signed long type.
If execution is stopped before a for script control statement, a variable specified in wordlists in the for statement has a fixed value. If you want to change the value of the variable that is assigned by the for statement, use the set command at the first stop location following do in the for statement to change the variable value, or use the set command before execution reaches the for statement to change the value of the variable in wordlists. The following shows examples.
- Example 1:
1: a=1 2: b=2 3: date 4: for num in $a $b 5: do 6: echo $num <--To change the value of num, execute the set command before line 6 executes. 7: pwd 8: done
- Example 2:
1: a=1 2: b=2 3: date <--To apply the values of $a and $b to the variable that is assigned by the for statement, execute the set command before line 3 executes. 4: for num in $a $b 5: do 6: echo $num 7: pwd 8: done
The command assigns to the left-hand term a numeric value stored in the variable specified in the right-hand term of an assignment expression as is (without rounding).
- Examples
To assign a numeric value, you must use the -i attribute of the typeset command to declare the variable's type as integer.
- Assign numeric value 10 to variable a
(adshdb) set a=10
- Assign character string test to variable b
(adshdb) set b="test"
- Assign the value of variable a to variable c
(adshdb) set c=a
- Assign numeric value 1 to variable d[5](array)
(adshdb) set d[5]=1
- If the value "2" is substituted in "e[5][1]" (array)
-
(adshdb) set e[5][1]=2