Search

Dark theme | Light theme

January 8, 2026

Nushell Niceties: Calculating The Average Of Numeric Values

In order to calculate an average for a list of numbers, file sizes, durations, or range of numbers, or a table with columns containing numeric values you can use the math avg command. This command is part of the math module of Nushell. When the input is a list then the result is a single value with the average of all values in the list. If you use a table as input the result is a record where the key is the column name and the value the average of all values in that column. Finally it is possible to have a single value as input and the result is the same value obviously.

The following example has several uses of the math avg command:

use std/assert

# Using math avg on a list of numbers.
assert equal ([1 2 3] | math avg) 2

# math avg also works on a single value
# and returns simply the value.
assert equal (1 | math avg) 1

# Using math avg on a range of numbers.
assert equal (5..10 | math avg) 7.5

# Using math avg on a list of durations.
assert equal ([1min 15sec 1.5min] | math avg) 55sec

# math avg also works on a single value
# and returns simply the value.
assert equal (1min | math avg) 1min

# Using math avg on a list of sizes.
assert equal ([200MB 100MB 0.6GB] | math avg) 300MB

# math avg also works on a single value
# and returns simply the value.
assert equal (100MB | math avg) 100MB

# Helper table with columns name, age, height (in cm).
let table = [
    [name age height];
    [Alice 30 175]
    [Bob 25 185]
    [Charlie 35 180]
]

# Using math avg on a table result in a record with for each
# numeric column the avg of all values.
assert equal ($table | math avg) { age: 30 height: 180 }

Written with Nushell 0.109.1.