Extending a list in Elvish shell (modifying $PATH)

275 Views Asked by At

By default Elvish (macOS, Homebrew) has a basic PATH definition:

~> echo $paths
[/usr/bin /bin /usr/sbin /sbin]

I'd like to add /usr/local/bin and /usr/local/sbin items to it, but without writing literals from scratch like

set paths = [/usr/bin /bin /usr/sbin /sbin /usr/local/bin /usr/local/sbin]

Basic pattern for more common shells is, of course

export PATH="/usr/local/bin:/usr/local/sbin:$PATH"

Lists in Elvish are immutable, so I'd like to do something like

set paths = paths + [/usr/local/bin /usr/local/sbin]

But I haven't find anything in docs, and my attempts fail:

~> set paths = paths + [/usr/local/bin /usr/local/sbin]
Exception: arity mismatch: assignment right-hand-side must be 1 value, but is 3 values
[tty 2]:1:1: set paths = paths + [/usr/local/bin /usr/local/sbin]

~> set paths = [$paths /usr/local/bin /usr/local/sbin]
Exception: path must be string
[tty 3]:1:5: set paths = [$paths /usr/local/bin /usr/local/sbin]
2

There are 2 best solutions below

0
On BEST ANSWER

You can either modify the path directly with set E:PATH = "/usr/local/bin:/usr/local/sbin:"$E:PATH, or use conj like this set paths = (conj $paths /usr/local/bin /usr/local/sbin).

0
On

Thanks to Pavlo and their notion of conj, I've found yet another way of doing it, so I'll list it here for completeness:

set paths = [/usr/local/bin /usr/local/sbin $@paths]

This upside here is an ability to prepend values to a list (conj does appending only), which is obviously important in the general case, but also in managing $PATH as well.