Home Explore Blog CI



nushell

2nd chunk of `book/special_variables.md`
5baf38243ccb17637a6e696daad5d00c6e209d0d0832b9200000000100000954
`$env` is a special mutable variable containing the current environment variables. As with any process, the initial environment is inherited from the parent process which started `nu`.

There are also several environment variables that Nushell uses for specific purposes:

### `$env.config`

`$env.config` is the main configuration record used in Nushell. Settings are documented in `config nu --doc`.

### `$env.PATH`

The search path for executing other applications. It is initially inherited from the parent process as a string, but converted to a Nushell `list` at startup for easy access.

It is converted back to a string before running a child-process.

### `$env.ENV_CONVERSIONS`

Allows users to specify how to convert certain environment variables to Nushell types. See [ENV_CONVERSIONS](./configuration.md#env-conversions).

### `$env.LAST_EXIT_CODE`

The exit code of the last command, usually used for external commands — Equivalent to `$?` from POSIX. Note that this information is also made available to the `catch` block in a `try` expression for external commands. For instance:

```nu
^ls file-that-does-not-exist e> /dev/null
$env.LAST_EXIT_CODE
# => 2

# or
try {
  ^ls file-that-does-not-exist e> /dev/null
} catch {|e|
  print $e.exit_code
}
# => 2
```

### `$env.CMD_DURATION_MS`

The amount of time in milliseconds that the previous command took to run.

### `$env.NU_VERSION`

The current Nushell version. The same as `(version).version`, but, as an environment variable, it is exported to and can be read by child processes.

### `$env.CURRENT_FILE`

Inside a script, module, or sourced-file, this variable holds the fully-qualified filename. Note that this
information is also available as a constant through the [`path self`](/commands/docs/path_self.md) command.

### `$env.FILE_PWD`

Inside a script, module, or sourced-file, this variable holds the fully qualified name of the directory in which
the file resides. Note that this value is also available as a constant through:

```nu
path self | path dirname
```

### `$env.PROCESS_PATH`

When _executing a script_, this variable represents the name and relative path of the script. Unlike the two variables
above, it is not present when sourcing a file or importing a module.

Note: Also unlike the two variables above, the exact path (including symlinks) that was used to _invoke_ the file is returned.

Title: Detailed Explanation of `$env` and its Sub-Variables in Nushell
Summary
This section provides a detailed explanation of the `$env` variable in Nushell, which contains environment variables. It covers key sub-variables such as `$env.config` (main configuration), `$env.PATH` (executable search path), `$env.ENV_CONVERSIONS` (environment variable type conversions), `$env.LAST_EXIT_CODE` (exit code of the last command), `$env.CMD_DURATION_MS` (execution time of the previous command), `$env.NU_VERSION` (current Nushell version), `$env.CURRENT_FILE` (full path of the current script/module), `$env.FILE_PWD` (directory of the current script/module), and `$env.PROCESS_PATH` (path used to execute the current script).