From 7de06b4785aa87a796a8c4ae84bd65e6641dfca1 Mon Sep 17 00:00:00 2001 From: Joerg Schuetter Date: Sun, 8 Oct 2023 19:33:30 +0200 Subject: [PATCH] prevent same entry in $env.PATH (#1104) * 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 --- book/configuration.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/book/configuration.md b/book/configuration.md index 99e3ffca525..4479771e148 100644 --- a/book/configuration.md +++ b/book/configuration.md @@ -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: