A.2. Operators Operators are used universally for setting, modifying, and comparing information as it passes around the shell. A.2.1. Arithmetic The arithmetic operators allow mathematical calculations within the shell. Table A-1 describes the available arithmetic operators and shows how they are used with numbers. Table A-1. Arithmetic operatorsOperator | Description | Example | Result |
---|
+ | Add two numbers | 8+2 | 10 | - | Subtract one number from another Negate a number | 82 -8 | 6 -8 | * | Multiply two numbers | 8*2 | 16 | / | Divide one number by another | 8/2 | 4 | ( ) | Change calculation order and evaluate the enclosed first | (86)*2 | 4 | % | Remainder of a division (modulus) | 8%3 | 2 |
When multiple arithmetic operators are combined in a single expression, MSH follows a precedence sequence for evaluation. The unary - operator (negate a number) will always be evaluated first, followed by multiply (*), divide (/), remainder (%), add (+), and subtract (-). Parentheses ((expression)) can be used to override the default operator-evaluation order. Some arithmetic operators are overloaded for other types. In particular, when strings are used, the add and multiply operators act differently, as shown in Table A-2. Table A-2. Arithmetic operators on stringsOperator | Description | Example | Result |
---|
+ | Concatenate two strings | "a"+"b" | "ab" | * | Repeat a string | "c"*6 | "cccccc" |
A.2.2. Assignment Assignment operators are used for defining and updating variable values. Although most composite operations are designed for use on numbers, the += and *= operators can also be used on strings. For the examples in Table A-3, assume that the variable $a has already been assigned a value of 8. Table A-3. Assignment operatorsOperator | Description | Example | Result |
---|
= | Set a variable | $a=1 | $a is 1 | += | Add to a variable | $a+=3 | $a is 11 | -= | Subtract from a variable | $a-=2 | $a is 6 | *= | Multiply a variable | $a*=2 | $a is 16 | /= | Divide a variable | $a/=2 | $a is 4 | %= | Set a variable to the remainder of a division | $a%=3 | $a is 2 | ++ | Increment a number variable | $a++ ++$a | $a is 9, returns 8 $a is 9, returns 9 | | Decrement a number variable | $a $a | $a is 7, returns 8 $a is 7, returns 7 |
A.2.3. Negation Monad provides two versions of the unary logical not operator, as shown in Table A-4. Table A-4. Unary logial not operatorsOperator | Description |
---|
-not | Logical not | ! | Logical not |
A.2.4. General Comparison Comparison operators are frequently used in evaluating test conditions for loops and flow-control statements (see Table A-5). Table A-5. General comparison operatorsOperator | Description |
---|
-eq | Equal to | -lt | Less than | -gt | Greater than | -le | Less than or equal to | -ge | Greater than or equal to | -ne | Not equal to | -and | Logical AND | -or | Logical OR |
A.2.5. String Comparison All of the general comparison operators listed in Table A-5 act in a case-insensitive fashion and can be used meaningfully on strings. Table A-6 lists a number of additional string-specific comparison operators. Table A-6. String operatorsOperator | Description |
---|
-ieq | Equal to (ignore case) | -ilt | Less than (ignore case) | -igt | Greater than (ignore case) | -ile | Less than or equal to (ignore case) | -ige | Greater than or equal to (ignore case) | -ine | Not equal to (ignore case) | -ceq | Equal to (case-sensitive) | -clt | Less than (case-sensitive) | -cgt | Greater than (case-sensitive) | -cle | Less than or equal to (case-sensitive) | -cge | Greater than or equal to (case-sensitive) | -cne | Not equal to (case-sensitive) | -like | Compare strings by wildcard (ignore case) | -notlike | Compare strings by wildcard (ignore case) | -ilike | Compare strings by wildcard (ignore case) | -inotlike | Compare strings by wildcard (ignore case) | -clike | Compare strings by wildcard (case-sensitive) | -cnotlike | Compare strings by wildcard (case-sensitive) | -match | Compare strings using a regular expression | -notmatch | Compare strings using a regular expression | -imatch | Compare strings using a regular expression (ignore case) | -inotmatch | Compare strings using a regular expression (ignore case) | -cmatch | Compare strings using a regular expression (case-sensitive) | -cnotmatch | Compare strings using a regular expression (case-sensitive) |
A.2.6. Array-Containment Operators Instead of iterating through every element, use array-containment operators to easily determine whether a given array contains a specified element (see Table A-7). Table A-7. Array containment operatorsOperator | Description |
---|
-contains | Look for an element in an array | -icontains | Look for an element in an array (case-insensitive) | -ccontains | Look for an element in an array (case-sensitive) |
A.2.7. Bitwise Operators Bitwise operators are commonly used for comparing flags and bit masks (see Table A-8). Table A-8. Bitwise operatorsOperator | Description |
---|
-band | Bitwise AND | -bor | Bitwise OR | -bnot | Bitwise NOT |
|