Skip to content

Special variables comparisons #1867

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 117 additions & 0 deletions book/special_variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,54 @@ The `$nu` constant is a record containing several useful values:

`$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`.

::: tip In other shells and languages ...

::: tabs
@tab Nu

```nu
# Set an environment variable
$env.VARNAME = "Content"

# Access an environment variable
$env.VARNAME
# => Content

# View all environment variables
$env
```

@tab Bash/POSIX

````bash:no-line-numbers
# Set an environment variable
VARNAME="Content"
export VARNAME
# or
export VARNAME="Content"

# Access an environment variable
echo $VARNAME
# => Content

# View all environment variables
env

@tab PowerShell

```powershell
# Set an environment variable
$env:VARNAME = "Content"

# Access an environment variable
$env:VARNAME
# => Content
````

<!-- Please add additional languages -->

:::

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

### `$env.config`
Expand Down Expand Up @@ -81,6 +129,40 @@ try {
# => 2
```

::: tip In other shells and languages ...

::: tabs

@tab Nu

```nu
$env.LAST_EXIT_CODE
```

@tab Bash/POSIX

```bash
$?
```

@tab PowerShell

```powershell
# Code
$LASTEXITCODE

# Successful - true or false
$?
```

@tab Perl

```perl
$? >> 8
```

:::

### `env.CMD_DURATION_MS`

The amount of time in milliseconds that the previous command took to run.
Expand All @@ -103,13 +185,48 @@ the file resides. Note that this value is also available as a constant through:
path self | path dirname
```

::: tip In other shells and languages ...

::: tabs

@tab Nu

```nu
$env.FILE_PWD
```

@tab PowerShell

```powershell
$PSCommandPath
```

:::

### `$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.

::: tip In other shells and languages ...

::: tabs
@tab Nushell

```nu
print $env.PROCESS_PATH
```

@tab Bash

```bash
echo $0
```

:::

### `$env.NU_LIB_DIRS`

A list of directories which will be searched when using the `source`, `use`, or `overlay use` commands. See also:
Expand Down