Nushell
Get Nu!
Getting Started
  • The Nushell Book
  • Command Reference
  • Cookbook
  • Language Reference Guide
  • Contributing Guide
Blog
  • English
  • 中文
  • Deutsch
  • Français
  • Español
  • 日本語
  • Português do Brasil
  • Русский язык
  • 한국어
GitHub
Get Nu!
Getting Started
  • The Nushell Book
  • Command Reference
  • Cookbook
  • Language Reference Guide
  • Contributing Guide
Blog
  • English
  • 中文
  • Deutsch
  • Français
  • Español
  • 日本語
  • Português do Brasil
  • Русский язык
  • 한국어
GitHub
  • Introduction
  • Installation
    • Default Shell
  • Getting Started
    • Quick Tour
    • Moving Around the System
    • Thinking in Nu
    • Nushell Cheat Sheet
  • Nu Fundamentals
    • Types of Data
    • Loading Data
    • Data Formats
    • Working with SQLite
    • Pipelines
    • Working with Strings
    • Regular Expressions
    • Working with Binary Data
    • Working with Lists
    • Working with Records
    • Working with Numbers
    • Working with Tables
    • Working with Paths
    • Navigating and Accessing Structured Data
    • Special Variables
  • Programming in Nu
    • Custom Commands
    • Aliases
    • Operators
    • Variables
    • Control Flow
    • Scripts
    • Modules
      • Using Modules
      • Creating Modules
    • Overlays
    • Sorting
    • Testing your Nushell Code
    • Debugging Scripts
    • Language Server
    • Best Practices
  • Nu as a Shell
    • Configuration
    • Configuration Reference
    • Environment
    • Stdout, Stderr, and Exit Codes
    • Working with the Terminal
    • Running System (External) Commands
    • How to Configure 3rd Party Prompts
    • Directory Stack
    • Reedline, Nu's Line Editor
    • Custom Completions
    • Externs
    • Coloring and Theming in Nu
    • Hooks
    • Background Jobs
  • Coming to Nu
    • Coming from Bash
    • Coming from CMD.EXE
    • Coming from PowerShell
    • Nu map from other shells and domain specific languages
    • Nu Map from Imperative Languages
    • Nu Map from Functional Languages
    • Nushell operator map
  • Design Notes
    • How Nushell Code Gets Run
  • (Not So) Advanced
    • Standard Library
      • std/assert
      • std/bench
      • std/clip
      • std/config
      • std/dt
      • std/formats
      • std/help
      • std/input
      • std/iter
      • std/log
      • std/math
      • std/random
      • std/util
      • std/xml
      • std-rfc
    • Dataframes
      • Dataframe Operations
      • Column Selectors
      • Math Expressions
      • String Expressions
      • Dates and Times in Dataframes
      • Masks and Conditionals
      • Aggregations and Window Functions
    • Metadata
    • Creating Your Own Errors
    • Parallelism
    • Plugins
    • explore
    • Building TUIs with tui
    • MCP Server
  1. The Nushell Book
  2. (Not So) Advanced
  3. Standard Library

Standard Library

Nushell ships with a standard library of useful commands written in native Nu. By default, the standard library is loaded into memory (but not automatically imported) when Nushell starts. The exception is the small std/prelude module, which is imported automatically and provides the banner and pwd commands.

  • Overview
  • The prelude
  • Importing the Standard Library
    • Importing the whole library
    • Importing Submodules
  • Logging with std/log
  • The Standard Library Candidate Module
  • Disabling the Standard Library
  • Using std/dirs in Modules
  • Optimal Startup

Overview

The standard library is split into modules, and you import only the ones you need, as described below. Each module has its own page:

ModuleRecommended importContents
std/assertuse std/assertassert and its subcommands, for tests
std/benchuse std/benchbench, which times closures
std/clipuse std/clipclip copy52, clip paste52 and clip prefix, for the clipboard through the terminal
std/configuse std/configconfig dark-theme, config light-theme and config env-conversions
std/dirsuse std/dirsdirs and its subcommands, the directory stack
std/dtuse std/dt *datetime-diff and pretty-print-duration, for calendar differences between dates
std/formatsuse std/formats *from and to commands for JSONL, NDJSON and NDNUON
std/helpuse std/helpanother version of help and its subcommands
std/inputuse std/inputinput display, which shows the key presses Nushell receives
std/iteruse std/iteriter find, iter scan and other commands for working through lists
std/loguse std/loglog info, log warning and the other logging commands
std/mathuse std/maththe constants E, PI, TAU, PHI and GAMMA
std/randomuse std/randomrandom dice
std/testinguse std/testing *the @test, @ignore, @before-each, @before-all, @after-each and @after-all attributes
std/utiluse std/util *path add, repeat, null-device, structure, ellie and the null_device constant
std/xmluse std/xml *xaccess, xupdate, xinsert and xtype, for XML data

Nushell also ships std-rfc, a library of candidate commands for the standard library. See The Standard Library Candidate Module.

To see a complete list of the commands available in the standard library, run the following:

nu -c "
  use std
  scope commands
  | where name =~ '^std '
  | select name description extra_description
  | wrap 'Standard Library Commands'
  | table -e
"

Note

The use std command above loads the entire standard library so that you can see all of the commands at once. This is typically not how it will be used (more info below). It is also run in a separate Nu subshell simply so that it is not loaded into scope in the shell you are using.

The list leaves a few things out:

  • The std/clip commands, which use std doesn't import.
  • The std/testing attributes. They are aliases, so they appear in scope aliases instead, as std testing attr test and so on.
  • The shells-aliases of std/dirs, which only use std/dirs shells-aliases * imports.
  • The std/math constants and null_device, which are values. After use std, they are in the $std record.

The prelude

Nushell imports std/prelude when it starts, unless you start it with --no-std-lib. The prelude has two commands.

pwd returns the current directory, $env.PWD. With --physical (-P), it resolves symbolic links first. On macOS, where /tmp is a link to /private/tmp:

cd /tmp
pwd
# => /tmp
pwd --physical
# => /private/tmp

banner returns the welcome message that Nushell shows at startup: Ellie, the version, some links, how long ago Nushell's first commit was, and the startup time. --short returns only the Startup Time line, and --no-startup-time returns everything except that line. Three keys of $env.config.color_config set its colors: banner_foreground for the text, which defaults to attr_normal, and banner_highlight1 and banner_highlight2 for the highlighted words, which default to green and purple. When config use-colors returns false, banner strips all ANSI codes from its text.

$env.config.show_banner decides what the REPL shows when it starts:

ValueShows
true or "full"the whole banner (the default)
"short"only the startup time line, banner --short
false or "none"nothing

The REPL prints the welcome part before it runs the startup hooks, and the startup time line just before the first prompt. The time it shows covers everything up to that prompt. See Remove Welcome Message.

Importing the Standard Library

The Standard Library modules and submodules are imported with the use command, just as any other module. See Using Modules for more information.

Importing the whole library

While working at the commandline, it can be convenient to load the entire standard library using:

use std *

However, this form should be avoided in custom commands and scripts since it has the longest load time.

Optimal Startup when Using the Standard Library

See the notes below on how to ensure that your configuration isn't loading the entire Standard Library.

use std and use std * both import every module except std/clip, which you have to import on its own. Both also run the export-env blocks of std/log and std/dirs, which set up the log formats and the directory stack. They differ in how the commands are named:

  • use std puts everything under the std prefix, as in std log info, std dirs add and std help. The std/util commands lose their module's name. They are std path add, std repeat, std null-device, std structure and std ellie, not std util path add. The math constants and null_device are in a $std record, as $std.math.PI and $std.null_device.
  • use std * imports each module under its own name, as in log info, dirs add and assert equal, and the std/util commands without any prefix, such as path add. The constants are $math.PI and $null_device.

use std * also brings in the help command of std/help, which replaces the built-in help and the page that the --help flag of every command shows. use std doesn't change help, because its version is named std help.

Importing Submodules

Each submodule of the standard library can be loaded separately. Again, for best performance, load only the submodule(s) that you need in your code.

See Importing Modules for general information on using modules. The table above shows the recommended import for each of the Standard Library submodules. It uses one of the first two forms below.

1. Submodules with <command> <subcommand> form

These submodules are normally imported with use std/<submodule> (without a glob/*): std/assert, std/bench, std/clip, std/config, std/dirs, std/help, std/input, std/iter, std/log and std/random. Their commands keep the submodule's name, as in log warning <msg>. std/math also works this way. After use std/math, its constants are $math.E and so on.

2. Import the definitions (contents) of the module directly

Some submodules are easier to use when their definitions (commands, aliases, constants, etc.) are loaded into the current scope. For instance:

use std/formats *
ls | to jsonl

The submodules that are normally imported with use std/<submodule> * (with a glob/*) are std/dt, std/formats, std/testing, std/util and std/xml. The glob matters most for std/testing, whose @test and other attributes only work without a prefix, and for std/formats, whose commands open and save find only under their plain names. std/util also exports a constant, null_device, for places where Nushell needs a value while it parses your code. use std/math * imports the math constants without a prefix, such as $E, though the prefixed form #1 above is usually easier to read.

3. use std <submodule>

It is possible to import Standard Library submodules using a space-separated form:

use std formats *

Important

As mentioned in Using Modules, this form (like use std *) first loads the entire Standard Library into scope and then imports the submodules. In contrast, the slash-separated versions in #1 and #2 above only import the submodule and will be much faster as a result.

This form also doesn't work for submodules that have a command of the same name. For example, use std log imports only the log command itself, not log info or the other subcommands. Use use std/log instead.

Logging with std/log

The logging commands, such as log info and log warning, are described in std/log, along with log levels, the format of a log line and custom levels.

The Standard Library Candidate Module

std-rfc, found in the nushell Repository, serves as a staging ground for possible Standard Library additions.

std-rfc ships with Nushell, so its submodules can be imported the same way as the Standard Library's, for example use std-rfc/str. It currently includes the conversions, date, iter, kv, path, pb, random, str, tables, url, and xml submodules. Their commands are described in std-rfc, which also covers the universal variables of std-rfc/kv.

Import std-rfc/date and std-rfc/kv with a glob. Their commands already start with the module's name, so use std-rfc/date gives you date date floor, and use std-rfc/kv gives you kv kv set. Importing on the std-rfc page lists the recommended import for every submodule. std-rfc/kv exists only in a Nushell built with the sqlite feature, which the default build includes.

use std-rfc imports every submodule except std-rfc/date, under the std-rfc prefix. Like use std, it drops the name of some submodules. The commands of conversions, tables and path become std-rfc into list, std-rfc with-extension and so on, while the others keep it, as in std-rfc str dedent.

If you are interested in adding to the Standard Library, please submit your code via PR to the std-rfc module in that repository. We also encourage you to try these candidate commands and provide feedback on them.

More details

Candidate commands for the Standard Library should, in general:

  • Have broad appeal - Be useful to a large number of users or use cases
  • Be well-written and clearly commented for future maintainers
  • Implement help comments with example usage
  • Have a description that explains why you feel the command should be a part of the standard library. Think of this as an "advertisement" of sorts to convince people to try the command and provide feedback so that it can be promoted in the future.

In order for a command to be graduated from RFC to the Standard Library, it must have:

  • Positive feedback
  • Few (or no) outstanding issues and, of course, no significant issues
  • A PR author for the std submission. This does not necessarily have to be the original author of the command.
  • Test cases as part of the std submission PR

Ultimately a member of the core team will decide when and if to merge the command into std based on these criteria.

Of course, if a candidate command in std-rfc no longer works or has too many issues, it may be removed from or disabled in std-rfc.

Disabling the Standard Library

To disable the standard library, you can start Nushell using:

nu --no-std-lib

This can be especially useful to minimize overhead when running a command in a subshell using nu -c. With -c, $nu.startup-time shows how long Nushell took to start before running the command, so you can compare the two:

nu --no-std-lib -n -c "$nu.startup-time"
# => 9ms 650µs 250ns

nu -n -c "$nu.startup-time"
# => 11ms 558µs 83ns

You will not be able to import the library, any of its submodules, nor use any of its commands, when it is disabled in this way. std-rfc is disabled as well, so use std/log and use std-rfc/str both fail with Module not found.

The banner and pwd commands from std/prelude are gone too, so Nushell runs them as external commands. pwd runs a pwd program from your PATH, such as /bin/pwd. On macOS, banner runs /usr/bin/banner, which prints text in large letters. Where no such program exists, the command isn't found. The REPL also skips its startup banner, whatever show_banner says.

Using std/dirs in Modules

Two standard library modules set environment variables in an export-env block:

  • std/log sets NU_LOG_FORMAT and NU_LOG_DATE_FORMAT, unless they already have a value.
  • std/dirs creates DIRS_LIST and DIRS_POSITION, the directory stack, and adds an ENV_CONVERSIONS entry for each of them.

If your own module imports one of them with a top-level use, that block doesn't run when your module is imported. See this caveat in the "Creating Modules" chapter for why.

std/log doesn't need the block. Its commands fall back to the default formats when the variables aren't set.

std/dirs does. Without the variables, dirs fails with Cannot find column 'DIRS_LIST'. Run the block from your module's own export-env, with use std/dirs [], which imports nothing but the environment:

# go.nu
use std/dirs

export-env {
    use std/dirs []
}

export def --env stack [] {
    dirs | length
}
use go.nu
go stack
# => 1

Optimal Startup

If Nushell's startup time is important to your workflow, review your startup configuration in config.nu, env.nu, and potentially others for inefficient use of the standard library. The following command should identify any problem areas:

view files
| enumerate | flatten
| where filename !~ '^std'
| where filename !~ '^repl_entry'
| where {|file|
    (view span $file.start $file.end) =~ 'use\s+std(\s|;|$)'
  }

Edit those files to use the recommended syntax in the Importing Submodules section above.

Note

If a Nushell library (e.g., from the nu_scripts repository), example, or doc is still using this syntax, please report it via an issue or PR.

If a third-party module is using this syntax, please report it to the author/maintainers to update.

Edit this page on GitHub
Contributors: Máté FARKAS, kai-tub, NotTheDr01ds, bfeitknecht, Jan Klass, fdncred
Prev
(Not so) Advanced
Next
Dataframes