Shell expressions consist of a set of words. Each operand and each operator must be a separate word. Sub-expressions can be enclosed in parenthesis which also appear as separate words. The following tables lists the supported operators. If nothing is said the listed operators have the same semantics as in the C/Java language. The higher the row the stronger is the operator binding. Operators are usually work on integer values. If an operator works also on strings its meaning is explained.
Refer to the following sections for description of expressions:
The characters "-", "!" and "~" are used as unary operators with the JCShell. They operate on numeric values only.
Example
/set-var answer 1
if ! ${answer}
...
| Operator | Description | Numeric only | Precedence |
|---|---|---|---|
| * | Multiplication (*) | X | 1 |
| / | Division (/) | X | |
| % | Remainder | X | |
| + | Addition (+) | X | 2 |
| - | Subtraction (-) | X | |
| << | Left shift | X | 3 |
| >> | Right shift with sign extension | X | |
| >>> | right shift with zero extension | X | |
| < <= > >= | Less than, less than or equal, greater than, greater than or equal. If both operands are numbers, a numeric comparison is performed. Otherwise, an alphanumeric comparison is done. | 4 | |
| == | Equal (have identical values) | 5 | |
| != | Not equal (have different values) | 5 | |
| =~ | The operator =~ checks is the left operand (string) matches regular expression on the right side (string). This operator is only defined on strings. | 5 | |
| & | Bitwise AND operation | X | 6 |
| ^ | Bitwise XOR operation | X | 7 |
| | | Bitwise OR operation | X | 8 |
| && | Conditional AND operation | X | 9 |
| || | Conditional OR operation | X | 10 |
All expression evaluation produces strings as result. An expression consisting of a single word evaluates to this word. In a boolean context, an expression evaluates to true if it has non-zero length and does not equal to "0".
Examples:
/set-var result ( ${num} * 5 ) - 1
${num} & 0xFF
if "${str}" == "IBM"
if "${str}" =~ "*IBM*"
${num} >= 5 && ${num} <= 10
In addition to the operators listed above, the JCShell allows to concatenated string. This operation is implicit, as the JCShell does not contain an explicit operator.
Examples:
/set-var x1 Hello
/set-var g1 ${x1}":its my text"
/echo ${g1}
Hello:its my text
Related information