File size: 10,161 Bytes
8d756d3 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
# Environment
A common task in a shell is to control the environment that external applications will use. This is often done automatically, as the environment is packaged up and given to the external application as it launches. Sometimes, though, we want to have more precise control over what environment variables an application sees.
You can see the current environment variables in the $env variable:
```nu
~> $env | table -e
โญโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฎ
โ โ โญโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฎ โ
โ ENV_CONVERSIONS โ โ โ โญโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโฎ โ โ
โ โ โ PATH โ โ from_string โ <Closure 32> โ โ โ
โ โ โ โ โ to_string โ <Closure 34> โ โ โ
โ โ โ โ โฐโโโโโโโโโโโโโโดโโโโโโโโโโโโโโโฏ โ โ
โ โ โ โ โญโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโฎ โ โ
โ โ โ Path โ โ from_string โ <Closure 36> โ โ โ
โ โ โ โ โ to_string โ <Closure 38> โ โ โ
โ โ โ โ โฐโโโโโโโโโโโโโโดโโโโโโโโโโโโโโโฏ โ โ
โ โ โฐโโโโโโโดโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏ โ
โ HOME โ /Users/jelle โ
โ LSCOLORS โ GxFxCxDxBxegedabagaced โ
| ... | ... |
โฐโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโดโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏ
```
In Nushell, environment variables can be any value and have any type. You can see the type of an env variable with the describe command, for example: `$env.PROMPT_COMMAND | describe`.
To send environment variables to external applications, the values will need to be converted to strings. See [Environment variable conversions](#environment-variable-conversions) on how this works.
The environment is initially created from the Nu [configuration files](configuration.md) and from the environment that Nu is run inside of.
## Setting environment variables
There are several ways to set an environment variable:
### $env.VAR assignment
Using the `$env.VAR = "val"` is the most straightforward method
```nu
> $env.FOO = 'BAR'
```
So, if you want to extend the Windows `Path` variable, for example, you could do that as follows.
```nu
$env.Path = ($env.Path | prepend 'C:\path\you\want\to\add')
```
Here we've prepended our folder to the existing folders in the Path, so it will have the highest priority.
If you want to give it the lowest priority instead, you can use the [`append`](/commands/docs/append.md) command.
### [`load-env`](/commands/docs/load-env.md)
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](environment.md#defining-environment-from-custom-commands) 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](variables_and_subexpressions.md#variable-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`:
```
> $env.FOO
BAR
> if "FOO" in $env {
> echo $env.FOO
> }
BAR
```
## 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
```
## Changing directory
Common task in a shell is to change directory with 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.
## Permanent environment variables
You can also set environment variables at startup so they are available for the duration of Nushell running.
To do this, set an environment variable inside [the Nu configuration file](configuration.md).
For example:
```nu
# In config.nu
$env.FOO = 'BAR'
```
## Defining environment from custom commands
Due to the scoping rules, any environment variables defined inside a custom command will only exist inside the command's scope.
However, a command defined as [`def --env`](/commands/docs/def.md) instead of [`def`](/commands/docs/def.md) (it applies also to [`export def`](/commands/docs/export_def.md), see [Modules](modules.md)) will preserve the environment on the caller's side:
```nu
> def --env foo [] {
$env.FOO = 'BAR'
}
> foo
> $env.FOO
BAR
```
## Environment variable conversions
You can set the `ENV_CONVERSIONS` environment variable to convert other environment variables between a string and a value.
For example, the [default environment config](https://github.com/nushell/nushell/blob/main/crates/nu-utils/src/sample_config/default_env.nu) includes conversion of PATH (and Path used on Windows) environment variables from a string to a list.
After both `env.nu` and `config.nu` are loaded, any existing environment variable specified inside `ENV_CONVERSIONS` will be translated according to its `from_string` field into a value of any type.
External tools require environment variables to be strings, therefore, any non-string environment variable needs to be converted first.
The conversion of value -> string is set by the `to_string` field of `ENV_CONVERSIONS` and is done every time an external command is run.
Let's illustrate the conversions with an example.
Put the following in your config.nu:
```nu
$env.ENV_CONVERSIONS = {
# ... you might have Path and PATH already there, add:
FOO : {
from_string: { |s| $s | split row '-' }
to_string: { |v| $v | str join '-' }
}
}
```
Now, within a Nushell instance:
```nu
> with-env { FOO : 'a-b-c' } { nu } # runs Nushell with FOO env. var. set to 'a-b-c'
> $env.FOO
0 a
1 b
2 c
```
You can see the `$env.FOO` is now a list in a new Nushell instance with the updated config.
You can also test the conversion manually by
```nu
> do $env.ENV_CONVERSIONS.FOO.from_string 'a-b-c'
```
Now, to test the conversion list -> string, run:
```nu
> nu -c '$env.FOO'
a-b-c
```
Because `nu` is an external program, Nushell translated the `[ a b c ]` list according to `ENV_CONVERSIONS.FOO.to_string` and passed it to the `nu` process.
Running commands with `nu -c` does not load the config file, therefore the env conversion for `FOO` is missing and it is displayed as a plain string -- this way we can verify the translation was successful.
You can also run this step manually by `do $env.ENV_CONVERSIONS.FOO.to_string [a b c]`
_(Important! The environment conversion string -> value happens **after** the env.nu and config.nu are evaluated. All environment variables in env.nu and config.nu are still strings unless you set them manually to some other values.)_
## Removing environment variables
You can remove an environment variable only if it was set in the current scope via [`hide-env`](/commands/docs/hide_env.md):
```nu
> $env.FOO = 'BAR'
...
> hide-env FOO
```
The hiding is also scoped which both allows you to remove an environment variable temporarily and prevents you from modifying a parent environment from within a child scope:
```nu
> $env.FOO = 'BAR'
> do {
hide-env FOO
# $env.FOO does not exist
}
> $env.FOO
BAR
```
|