Operators

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

OperatorDescription
+add
-subtract
multiply
/divide
*exponentiation (power)
modmodulo
==equal
!=not equal
<less than
<=less than or equal
>greater than
>=greater than or equal
=~regex match / string contains another
!~inverse regex match / string does not contain another
invalue in list
not-invalue not in list
&&and two Boolean values
||or two Boolean values

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

Order of operations

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

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

Regular Expression / string-contains Operators

The =~ and !~ operators provide a convenient way to evaluate regular expressionsOperators - 图1 (opens new window). You don’t need to know regular expressions to use them - they’re also an easy way to check whether 1 string contains another.

  • string =~ pattern returns true if string contains a match for pattern, and false otherwise.
  • string !~ pattern returns false if string contains a match for pattern, and true otherwise.

For example:

  1. foobarbaz =~ bar # returns true
  2. foobarbaz !~ bar # returns false
  3. ls | where name =~ ^nu # returns all files whose names start with "nu"

Both operators use the Rust regex crate’s is_match() functionOperators - 图2 (opens new window).

Case Sensitivity

Operators are usually case-sensitive when operating on strings. There are a few ways to do case-insensitive work instead:

  1. In the regular expression operators, specify the (?i) case-insensitive mode modifier:
  1. "FOO" =~ "foo" # returns false
  2. "FOO" =~ "(?i)foo" # returns true
  1. Use the str contains command’s --insensitive flag:
  1. "FOO" | str contains --insensitive "foo"
  1. Convert strings to lowercase with str downcase before comparing:
  1. ("FOO" | str downcase) == ("Foo" | str downcase)