Home Explore Blog CI



nushell

1st chunk of `book/operators.md`
f03e214e9accf79c18669d00b9a3942098f38807f10f2c380000000100000fa7
# Operators

Nushell supports the following operators for common math, logic, and string operations:

| Operator           | Description                                             |
| ------------------ | ------------------------------------------------------- |
| `+`                | add                                                     |
| `-`                | subtract                                                |
| `*`                | multiply                                                |
| `/`                | divide                                                  |
| `//`               | floor division                                          |
| `mod`              | modulo                                                  |
| `**`               | exponentiation (power)                                  |
| `==`               | equal                                                   |
| `!=`               | not equal                                               |
| `<`                | less than                                               |
| `<=`               | less than or equal                                      |
| `>`                | greater than                                            |
| `>=`               | greater than or equal                                   |
| `=~` or `like`     | regex match / string contains another                   |
| `!~` or `not-like` | inverse regex match / string does *not* contain another |
| `in`               | value in list                                           |
| `not-in`           | value not in list                                       |
| `has`              | list has value                                          |
| `not-has`          | list does not have value                                |
| `not`              | logical not                                             |
| `and`              | and two Boolean expressions (short-circuits)            |
| `or`               | or two Boolean expressions (short-circuits)             |
| `xor`              | exclusive or two boolean expressions                    |
| `bit-or`           | bitwise or                                              |
| `bit-xor`          | bitwise xor                                             |
| `bit-and`          | bitwise and                                             |
| `bit-shl`          | bitwise shift left                                      |
| `bit-shr`          | bitwise shift right                                     |
| `starts-with`      | string starts with                                      |
| `ends-with`        | string ends with                                        |
| `++`               | append lists                                            |


Parentheses can be used for grouping to specify evaluation order or for calling commands and using the results in an expression.

## Order of Operations

To understand the precedence of operations, you can run the command: `help operators | sort-by precedence -r`.

Presented in descending order of precedence, the article details the operations as follows:

- Parentheses (`()`)
- Exponentiation/Power (`**`)
- Multiply (`*`), Divide (`/`), Integer/Floor Division (`//`), and Modulo (`mod`)
- Add (`+`) and Subtract (`-`)
- Bit shifting (`bit-shl`, `bit-shr`)
- Comparison operations (`==`, `!=`, `<`, `>`, `<=`, `>=`), membership tests (`in`, `not-in`, `starts-with`, `ends-with`), regex matching (`=~`, `!~`), and list appending (`++`)
- Bitwise and (`bit-and`)
- Bitwise xor (`bit-xor`)
- Bitwise or (`bit-or`)
- Logical and (`and`)
- Logical xor (`xor`)
- Logical or (`or`)
- Assignment operations
- Logical not (`not`)

```nu
3 * (1 + 2)
# => 9
```

## Types

Not all operations make sense for all data types.
If you attempt to perform an operation on non-compatible data types, you will be met with an error message that should explain what went wrong:
```nu
"spam" - 1
# => Error: nu::parser::unsupported_operation (link)

Title: Nushell Operators and Order of Operations
Summary
This section details the operators supported in Nushell for math, logic, string operations, and list manipulation, including their descriptions. It also covers the order of operations, listing them from highest to lowest precedence, and clarifies that using incompatible data types with certain operations will result in an error.