The math module in Nushell has a lot of useful commands to work with numeric values.
You can use the math sum command to calculate the sum of a multiple numeric values.
The input of the command can be a list of numbers, durations, file sizes, or a range or table with columns containing numeric values.
The result is a single numeric value with the sum of all values in the input.
The math sum command can also be used on a table with multiple numeric columns.
It will return a record with the sum of all values for each column.
In the following example you can see several uses of the math sum command with different input types:
use std/assert
# Using math sum on a list of numbers.
assert equal ([1 2 3] | math sum) 6
# Using math sum on a range of numbers.
assert equal (5..10 | math sum) 45
# Using math sum on a list of durations.
assert equal ([1min 15sec 1.5min] | math sum) 165sec
# Using math sum on a list of sizes.
assert equal ([200MB 100MB 0.5GB] | math sum) 800MB
# Helper table with columns name, age, height (in cm).
let table = [
[name age height];
[Alice 30 175]
[Bob 25 183]
[Charlie 35 180]
]
# Using math sum on a table result in a record with for each
# numeric column the sum of all values.
assert equal ($table | math sum) { age: 90 height: 538 }
Written with Nushell 0.109.1.