A variable is a place to store some data. You can change the data assigned to a variable (ie, change its value). Variables are therefore very useful for keeping track of data that changes during the script's execution.


Variable names

Each variable must have a unique name, and you access its value by using that name. When you choose a name for a variable, the first character must be one of the letters A through Z, !, ?, @, #, $, or _. Any remaining characters of the variable's name can be any of the preceding characters as well as 0 to 9. The case of letters is unimportant, so that for example, Hello, HELLO, and hellO all are the same variable name.

Note: Although you can name a variable the same as any REXX keyword, REXX function name, or subroutine in your script, you should avoid doing so. Also, don't use the names RESULT, RC, or SIGL as these are special variables maintained by REXX for your script's use. To do otherwise will interfere with the interpreter maintaining these special variables for your use.

Note: You can use the SYMBOL() built-in function to check if a variable name is legal.


Assigning a value via an assignment statement

You can assign a value to a variable simply by writing a REXX instruction that consists of the variable name, followed by an equal sign, and then the desired value for the variable. We call this an assignment statement. (You can also assign a value to a variable via the PARSE, ARG, PULL, or DO statements, each discussed later).

Here we have a variable named My_Variable. We will set it to the value of 10.

My_Variable = 10
You can change the value assigned to a particular variable (name) at any time, For example, here we change the value of that same variable above to the value of 5.
My_Variable = 5
Every time that REXX encounters what it regards to be a variable name in some instruction, REXX replaces the variable name with its value when carrying out that instruction. For example, if you subsequently use the above variable name as an argument for the SAY instruction, REXX will substitute its value.
SAY My_Variable
The above would print out 5 upon the screen. It would not print out My_Variable. Since My_Variable is not a literal string enclosed in quotes nor an unquoted number (nor a function name, nor anything else that REXX recognizes), then REXX regards it as a variable name. Therefore, REXX substitutes the value of My_Variable as the real item to SAY here. We already told REXX that My_Variable's value is 5 with the preceding assignment instruction.

A variable can be assigned a value consisting of some literal string. For example, here we set My_Variable to the string This is some text.

My_Variable = 'This is some text'
Now if you subsequently use this variable name as an argument for the SAY instruction, REXX will substitute its new value. For example:
SAY My_Variable
...will print out This is some text upon the screen. It would not print out My_Variable (since My_Variable is not a literal string enclosed in quotes), but instead, print the value of My_Variable which happens to be the string This is some text.

When assigning a value to a variable, that value may be any mathematical expression. For example:

My_Variable = 5 + 3
...sets the value of My_Variable to 8 (ie, the addition of 5 and 3). Since quotes weren't placed around 5 + 3, it's not one, literal string and REXX therefore evaluates the mathematical expression 5 + 3 rather than literally accepting it as you typed it. Of course, if you did really want to set My_Variable to the string 5 + 3, you'd specify a literal string by enclosing the math operator in quotes too, as so:
My_Variable = '5 + 3'
Furthermore, a variable's value can be set to a concatenation of strings, as so:
My_Variable = "My name is " || "Jeff"
If you subsequently use My_Variable above as an argument for the SAY instruction, SAY will print the combined string of My name is Jeff, which is the current value of My_Variable.

You can set a variable to the value of another variable, as so:

My_Variable = 5
My_Other_Variable = My_Variable
Above, both variables have the value of 5.

You can set a variable to the value of a mathematical expression where another variable is used in the expression. In other words, a variable can itself be used in a mathematical expression. REXX simply substitutes the variable's value, and then performs the math. The variable's value must be numeric.

For example, here we set My_Other_Variable to the value of My_Variable + 5.

My_Variable = 5
My_Other_Variable = My_Variable + 3
Above, My_Other_Variable has the value of 8.

Note: You can assign a variable a new value by also using its old value in an expression. For example, here's how to increment a variable by 1.

My_Variable = My_Variable + 1
The value of My_Variable on the right side of the equal sign is the value before 1 is added to it, and this new value is now assigned back to My_Variable.

You can even set a variable's value to a string that is the combination of several literal strings and mathematical expressions.

My_Variable = 5
My_Answer = "The answer to" My_Variable "+ 3 is" My_Variable + 3
The above results in My_Answer's value being set to the string The answer to 5 + 3 is 8. First of all, The answer to is a literal string, so that is retained verbatim. Next, REXX regards My_Variable as a variable name. (ie, It isn't enclosed in quotes, nor is it a keyword, label, function name, etc.), so REXX substitutes its value there, which is simply 5. Note that because a space was left inbetween the literal string The answer to and My_Variable, this space is retained in My_Answer's value. Next, + 3 is is another literal string. Finally, REXX sees another variable name, and notes that 3 is being added to it. Since this is not enclosed within quotes (ie, is not a literal string), REXX deems this to be a mathematical expression, substitutes the variable's value, and evaluates this mathematical expression to 8. Again, leaving a space inbetween + 3 is and that mathematical expression results in the space being retained in My_Answer's value.

We could have used the concatenation operator to combine the above pieces as so:

My_Variable = 5
My_Answer = "The answer to " || My_Variable || " + 3 is " || My_Variable + 3
Note that I added extra spaces to those literal strings because the concatenation operator appends the pieces together without otherwise retaining spaces inbetween. If you run into a situation where you don't want a space inbetween a variable's value and a literal string, then you must use the concatenation operator. For example, assume that the variable My_Program is already set to a string describing the name of some executable, and you wish to append the literal string .exe to it without a space inbetween, and assign this final string to the variable Full_Name:
Full_Name = My_Program || ".exe"
Furthermore, if you wish to combine the values of two variables without a space in between, then you must use the concatenation operator.
Full_Name = My_Program || My_Extension
You can even concatenate variables whose values are numeric, as if they were strings.
My_Variable = 1
My_Fraction = '.5'
My_Result = My_Variable || My_Fraction
SAY My_Result /* Prints 1.5 */

Variables without a value (DROP)

If you use a variable, but that variable has not been previously given a value, this does not cause an error (unless SIGNAL ON NOVALUE is used, explained later). Instead, REXX just uses the variable's own name (translated into upper case) as its value.

/* The variable My_Variable has not yet been assigned a value */
SAY My_Variable
The above results in the string MY_VARIABLE being displayed.

Some interpreters allow you to clear a variable's value (ie, set it to nul) by putting nothing to the right of the equal sign in an assignment statement. But a better, more supported alternative to clear a variable's value is to use the DROP instruction, as so:

DROP My_Variable
Now, it is as if My_Variable had never been assigned any value.

Note: You can use the SYMBOL() built-in function to check whether a variable's value is set (ie, not DROP'ed).


Errata

As you can see, variables can be assigned values that are numeric or non-numeric. So, how does REXX know whether a variable represents a numeric or non-numeric value? REXX automatically checks the value of a variable each time that it is used in an instruction to determine whether it's numeric or non-numeric. For example, if you assign the value 506 to a variable, REXX figures out that it's a numeric value whenever you use the variable. If you assign Hello to that same variable, REXX figures out that it's a non-numeric value whenever you use the variable. You can change a variable's value from numeric to non-numeric while the script executes, and REXX will follow along. This is why REXX variables are described as untyped -- because you can change their values from numeric to non-numeric and back "on the fly". But if a variable's value happens to be non-numeric, then you can't use the variable in a mathematical expression. Math operators work upon a variable only when its value is numeric (ie, its value consists only of the characters 0 through 9, plus optionally a leading plus or minus sign, or one decimal point, and an exponent). If you try to use a variable, whose value is not numeric, in a math expression, REXX will raise a "Bad arithmetic conversion" SYNTAX condition, or some such math related SYNTAX condition.

Note: You can use the DATATYPE() built-in function to check whether a variable's value is numeric before you attempt to use it in a mathematical expression.

Note: Some REXX documentation refers to variables as "symbols".