Operators
Nushell supports the following operators for common math, logic, and string operations:
Operator | Description |
---|---|
+ | add |
- | subtract |
| multiply |
/ | divide |
* | exponentiation (power) |
mod | modulo |
== | 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 |
in | value in list |
not-in | value 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 (-
)
> 3 * (1 + 2)
9
Regular Expression / string-contains Operators
The =~
and !~
operators provide a convenient way to evaluate regular expressions (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 ifstring
contains a match forpattern
, and false otherwise.string !~ pattern
returns false ifstring
contains a match forpattern
, and true otherwise.
For example:
foobarbaz =~ bar # returns true
foobarbaz !~ bar # returns false
ls | where name =~ ^nu # returns all files whose names start with "nu"
Both operators use the Rust regex crate’s is_match()
function (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:
- In the regular expression operators, specify the
(?i)
case-insensitive mode modifier:
"FOO" =~ "foo" # returns false
"FOO" =~ "(?i)foo" # returns true
- Use the str contains command’s
--insensitive
flag:
"FOO" | str contains --insensitive "foo"
- Convert strings to lowercase with str downcase before comparing:
("FOO" | str downcase) == ("Foo" | str downcase)