Home Explore Blog CI



nushell

2nd chunk of `book/environment.md`
8142ba4d597fc93083782056800e89a177ef540ba12030200000000100000d8f
If you have more than one environment variable you'd like to set, you can use [`load-env`](/commands/docs/load-env.md) to create a table of name/value pairs and load multiple variables at the same time:

```nu
load-env { "BOB": "FOO", "JAY": "BAR" }
```

### One-shot Environment Variables

These are defined to be active only temporarily for a duration of executing a code block.
See [Single-use environment variables](environment.md#single-use-environment-variables) for details.

### Calling a Command Defined with [`def --env`](/commands/docs/def.md)

See [Defining environment from custom commands](custom_commands.md#changing-the-environment-in-a-custom-command) for details.

### Using Module's Exports

See [Modules](modules.md) for details.

## Reading Environment Variables

Individual environment variables are fields of a record that is stored in the `$env` variable and can be read with `$env.VARIABLE`:

```nu
$env.FOO
# => BAR
```

Sometimes, you may want to access an environmental variable which might be unset. Consider using the [question mark operator](types_of_data.md#optional-cell-paths) to avoid an error:

```nu
$env.FOO | describe
# => Error: nu::shell::column_not_found
# => 
# =>   × Cannot find column
# =>    ╭─[entry #1:1:1]
# =>  1 │ $env.FOO
# =>    · ──┬─ ─┬─
# =>    ·   │   ╰── cannot find column 'FOO'
# =>    ·   ╰── value originates here
# =>    ╰────

$env.FOO? | describe
# => nothing

$env.FOO? | default "BAR"
# => BAR
```

Alternatively, you can check for the presence of an environmental variable with `in`:

```nu
$env.FOO
# => BAR

if "FOO" in $env {
    echo $env.FOO
}
# => BAR
```

### Case sensitivity

Nushell's `$env` is case-insensitive, regardless of the OS. Although `$env` behaves mostly like a record, it is special in that it ignores the case when reading or updating. This means, for example, you can use any of `$env.PATH`, `$env.Path`, or `$env.path`, and they all work the same on any OS.

If you want to read `$env` in a case-sensitive manner, use `$env | get --sensitive`.

## Scoping

When you set an environment variable, it will be available only in the current scope (the block you're in and any block inside of it).

Here is a small example to demonstrate the environment scoping:

```nu
$env.FOO = "BAR"
do {
    $env.FOO = "BAZ"
    $env.FOO == "BAZ"
}
# => true
$env.FOO == "BAR"
# => true
```

See also: [Changing the Environment in a Custom Command](./custom_commands.html#changing-the-environment-in-a-custom-command).

## Changing the Directory

A common task in a shell is to change the directory using the [`cd`](/commands/docs/cd.md) command. In Nushell, calling [`cd`](/commands/docs/cd.md) is equivalent to setting the `PWD` environment variable. Therefore, it follows the same rules as other environment variables (for example, scoping).

## Single-use Environment Variables

A common shorthand to set an environment variable once is available, inspired by Bash and others:

```nu
FOO=BAR $env.FOO
# => BAR
```

You can also use [`with-env`](/commands/docs/with-env.md) to do the same thing more explicitly:

```nu
with-env { FOO: BAR } { $env.FOO }
# => BAR
```

The [`with-env`](/commands/docs/with-env.md) command will temporarily set the environment variable to the value given (here: the variable "FOO" is given the value "BAR"). Once this is done, the [block](types_of_data.md#blocks) will run with this new environment variable set.

Title: Setting, Reading, Scoping, and Single-Use Environment Variables in Nushell
Summary
This section details various methods for setting environment variables, including `load-env` for multiple variables, one-shot variables, commands defined with `def --env`, and module exports. It covers reading variables using `$env.VARIABLE`, handling unset variables with the question mark operator, and checking for variable presence using `in`. The section also addresses case sensitivity, scoping of environment variables, changing directories with `cd`, and using single-use environment variables with `FOO=BAR` syntax and the `with-env` command.