Skip to content

Commit

Permalink
prevent same entry in $env.PATH (#1104)
Browse files Browse the repository at this point in the history
* prevent same entry in $env.PATH

if one extends PATH in `env.nu` and start a different shell before launching nu, one might start to collect the same path multiple times in PATH
these lines construct the new value (based on home directory and the separator based on the os)

* no need for env variable

Co-authored-by: Antoine Stevan <44101798+amtoine@users.noreply.github.com>

* simplification: make use of if .. in

Co-authored-by: Antoine Stevan <44101798+amtoine@users.noreply.github.com>

* remove mentions to `$env.my_path`

---------

Co-authored-by: Antoine Stevan <44101798+amtoine@users.noreply.github.com>
Co-authored-by: amtoine <stevan.antoine@gmail.com>
  • Loading branch information
3 people committed Oct 8, 2023
1 parent 43d5155 commit 7de06b4
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions book/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,14 @@ This will append `/some/path` to the end of PATH; you can also use [`prepend`](/

Note the `split row (char esep)` step. We need to add it because in `env.nu`, the environment variables inherited from the host process are still strings. The conversion step of environment variables to Nushell values happens after reading the config files (see also the [Environment](environment.html#environment-variable-conversions) section). After that, for example in the Nushell REPL when `PATH`/`Path` is a list , you can use [`append`](/commands/docs/append.md)/[`prepend`](/commands/docs/prepend.md) directly.

To prepend a new path only if not already listed, one can add to `env.nu`:
```nu
# create a new string holding the desired path
let my_path = ( $nu.home-path | path join "bin" )
# return $env.PATH if $my_path is already listed, return $env.PATH with $my_path prepended otherwise
$env.PATH = ( if $my_path in $env.PATH { $env.PATH } else { $env.PATH | prepend $my_path } )
```

### Homebrew

[Homebrew](https://brew.sh/) is a popular package manager that often requires PATH configuration. To add it to your Nushell PATH:
Expand Down

0 comments on commit 7de06b4

Please sign in to comment.