## With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

No credit card required

# Operators

Operators are used universally for setting, modifying, and comparing information as it passes around the shell.

## 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 operators

Operator

Description

Example

Result

+

8+2

10

-

Subtract one number from another Negate a number

8–2

-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

(8–6)*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 strings

Operator

Description

Example

Result

+

Concatenate two strings

"a"+"b"

"ab"

*

Repeat a string

"c"*6

"cccccc"

## Assignment

Assignment operators are used for defining and updating variable values. Although most composite operations are designed for ...

## With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

No credit card required