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
  • Categories

    • Bits
    • Bytes
    • Chart
    • Conversions
    • Core
    • Database
    • Dataframe
    • Dataframe Or Lazyframe
    • Date
    • Debug
    • Default
    • Env
    • Experimental
    • Expression
    • Filesystem
    • Filters
    • Formats
    • Generators
    • Hash
    • History
    • Lazyframe
    • Math
    • Misc
    • Network
    • Path
    • Platform
    • Plugin
    • Prompt
    • Random
    • Shells
    • Strings
    • System
    • Viewers

with-env for env

Runs a block with temporary environment variables. A config record updates the configuration for the duration of the block.

Command Type

built-in

Signature

> with-env {flags} (variable) (block)

Flags

  • --no-convert: Set the values as given, without $env.ENV_CONVERSIONS or making PATH a list.

Parameters

  • variable: The environment variable to temporarily set.
  • block: The block to run once the variable is set.

Input/output types:

inputoutput
anyany

Examples

Set by key-value record.

> with-env {X: "Y", W: "Z"} { [$env.X $env.W] }
╭───┬───╮
│ 0 │ Y │
│ 1 │ Z │
╰───┴───╯

Run a block with changed configuration.

> with-env {config: {float_precision: 4}} { $env.config.float_precision }
4

Set a variable, which its environment conversion turns into a list.

> $env.ENV_CONVERSIONS = {MY_ENV_VAR: {from_string: { split row ':' }}}; with-env {MY_ENV_VAR: 'foo:bar'} { $env.MY_ENV_VAR }
╭───┬─────╮
│ 0 │ foo │
│ 1 │ bar │
╰───┴─────╯

Set a variable as given, without converting it.

> $env.ENV_CONVERSIONS = {MY_ENV_VAR: {from_string: { split row ':' }}}; with-env --no-convert {MY_ENV_VAR: 'foo:bar'} { $env.MY_ENV_VAR }
foo:bar

Notes

The values are converted as load-env converts them: each string value with a from_string conversion in $env.ENV_CONVERSIONS is converted, and a string PATH becomes a list. FOO=bar command sets FOO this way too. With --no-convert, the values are set as given.