# Math

Sometimes you just need to add a few numbers when you're working on a task. Nu offers a set of basic math operations that you can use:

To get into "math mode", you start the command with an =. This lets Nu know what you're about to write will use operators. Some commands, like where will do this for you so that you don't have to.

# Add, Subtract, Multiply, Divide

> = 1 + 3

In Nu, you can do the usual add, subtract, multiply and divide with the operators +, -, *, and / respectively. Operator precedence is respected, so 1 + 2 * 3 will be treated as 1 + (2 * 3). Which leads us to parentheses.

# Parentheses

You can use parentheses to group math expression in math mode. This allows you to write (1 + 2) * 3 if you want the addition to have higher precedence.

# in and not-in

You can check if a value is in a set of values or not using the in and not-in operators.

> = 1 in [1 2 3]
> = 1 not-in [1 2 3]

# =~ and !~

You can check to see if a string is inside of another string, or not inside of another string, using =~ and !~.

> = "foobar" =~ "foo"
> = "foobar" !~ "baz"

# Comparisons

The following comparisons are also available:

  • < - less than
  • <= - less than or equal to
  • > - greater than
  • >= - greater than or equal to
  • == - equal to
  • != - not equal to

# Compound operators

Nushell also supports && and || to join two operations that return boolean values, using 'and' and 'or' respectively. For example: ls | where name in ["one" "two" "three"] && size > 10kb

# Order of operations

Math operations are evaluated in the follow order (from highest precedence to lowest):

  • Parentheses (())
  • Multiply (*) and Divide (/)
  • Add (+) and Subtract (-)
> = 3 * (1 + 2)

# Short-hand math mode

A variation of math mode that Nushell supports is called "short-hand" math mode. This is because it gives you a way of accessing columns using a simple short-hand.

You may have already used this functionality before. If, for example, we wanted to only see rows from ls where the entry is at least ten kilobytes, we can write:

> ls | where size > 10kb

The where size > 10kb is a command with two parts: the command names where and the short-hand math expression size > 10kb. We say short-hand because size here is the shortened version of writing $it.size. If we look at the fully expanded form, we would see:

> ls | where { = $it.size > 10kb }

The = gives us the full math mode. Rather than having to type all this out every time a command needs to work with column data, we use this short-hand mode to access column data.

For the expansion to work, the column must appear on the left-hand side of the operation. Above, size appears on the left-hand side of the comparison, which allows the expression to expand into the full math mode block.