Xshe – Cross-Shell Environment Vars

Fork me on GitHub Leave a GitHub Repo Star Open an Issue

xshe allows for setting Shell Environment variables across multiple shells with a single TOML configuration file.

Simply write lines in a xshe.toml file like this:

toml CARGO_HOME = "~/.cargo"

Create a file like this once and use it everywhere, for every shell! xshe can convert this format into the format for every supported shell.

Shells - bash | zsh | fish Coming Soon - elvish | dash | xonsh | tsch


GitHub Release Workflow Status Crates.io License Crates.io GitHub release (latest by date including pre-releases)


Installation

You can install xshe from Cargo (Rust's package manager) if you have it installed on your system. If you don't have Cargo or don't want to use it, you can also download the binaries for your system directly from GitHub, or install Cargo first before proceeding.

Note: After installing xshe, you might have to add the resulting xshe binary to your PATH. (what's that?)

With Cargo

Install xshe from crates.io with Cargo.

shell cargo install -f xshe

As a File Download

Instead of using Cargo, you can download the latest release binary that corresponds with your system (or view all releases).

Make sure to add the xshe binary to your PATH, or remember to use the full path to the binary whenever you run xshe.


Setup

Creating a xshe.toml file

Create a file called xshe.toml in ~/.config. This is a TOML file that represents environment variables.

An example configuration is here: xshe example

One variable is set per line. The file is read in order from top to bottom, so variables that appear earlier in the file can be used to define ones that appear later.

A typical line looks like this:

toml CARGO_HOME = "$XDG_DATA_HOME/cargo"

This will then be converted into the correct format for whatever shell is being used. For example, in bash, this line becomes:

bash export CARGO_HOME="$XDG_DATA_HOME/cargo"; While in fish, this line is: fish set -gx CARGO_HOME "$XDG_DATA_HOME/cargo";

Dealing with PATH variables

To set variables that are arrays of values, like $PATH, use this syntax:

toml PATH = ["$PATH", "$BIN_HOME", "$CARGO_HOME/bin"] xshe will join each element together based on the shell that is specified.

Shell Specific Environment Variables

To set environment variables for only one shell, add a new table called [shell.NAME] after all standard definitions, where NAME is on of bash, zsh, or fish. Then list the environment variables that will only be added if xshe is being used for the given shell.

For example, to make $HISTFILE be different between shells and $ZSH_CACHE_DIR only be set in zsh, do this: ```toml [shell.bash] HISTFILE = "$XDGSTATEHOME/bash_history"

[shell.zsh] HISTFILE = "$XDGSTATEHOME/zshhistory" ZSHCACHEDIR = "$XDGCACHE_HOME/oh-my-zsh" ```

Sourcing the xshe.toml file

Put the line corresponding to your shell in whatever file runs when loading environment variables. For bash, this is ~/.bash_profile, for zsh, this is ~/.zshenv, and for fish, this is ~/.config/fish/config.fish.

Bash

bash eval "$(xshe bash)"

Zsh

zsh eval "$(xshe zsh)"

Fish

fish eval "$(xshe fish)"

Use without xshe on PATH

If xshe isn't on your PATH (this is the cause of the error command not found: xshe), you will have to manually type out the location:

zsh eval "$(/path/to/xshe zsh)"

Options

Using --file (or -f)

To specify a custom file that is not located at ~/.config/xshe.toml, pass the --file option, like so:

zsh eval "$(xshe zsh --file ~/some/other/location.toml)"

Using --toml (or -t)

To directly specify TOML to parse as a config file, use --toml.

For example, this line directly parses the provided line and converts it to zsh: zsh xshe zsh --toml 'BIN_HOME = "$HOME/.local/bin"'


License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.


built with love