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:
| input | output |
|---|---|
| any | any |
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 }
4Set 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:barNotes
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.