#shell #env-var #shell-environment #bash #zsh #batch #ksh


Helpers for writing tools that will be eval'd by shells

3 releases

Uses old Rust 2015

0.1.2 May 19, 2018
0.1.1 Feb 24, 2016
0.1.0 Feb 24, 2016

#938 in Filesystem

Download history 22/week @ 2024-02-18 35/week @ 2024-02-25 8/week @ 2024-03-03 14/week @ 2024-03-10 2/week @ 2024-03-17 49/week @ 2024-03-31 3/week @ 2024-04-07

52 downloads per month

CC0 license

101 lines

It's well known that a program cannot modify the environment of its parent shell. But this is useful to do, and we can use some tricks to do this. Almost all shells support some way to evaluate the output of programs (even Windows), so by returning the right commands to be eval'd by the parent shell, we can apply these changes.


Add the following to your Cargo.toml file:

setenv = "0.1"


This library provides two things:

  1. Some code to try to detect what shell is in use
  2. What syntax is needed to do certain actions.

At the moment, the only two commands supported are cd for changing directories, and setenv for setting environment variables.

Two other functions are also provided as a convienence: split_env which is just a wrapper around std::env::split_paths, and set_env_list which is a wrapper around std::env::join_paths.


To make use of all this, each executable using setenv should be wrapped in an alias/function/bat file. Here are some examples:


for /f "tokens=*" %%I in ('d:\target\debug\myapp.exe  %*') do (


function dothing() {
  eval `/target/debug/myapp "$@"`


dothing() {
  eval `/target/debug/myapp "$@"`


function dothing() {
  eval `/target/debug/myapp $*`


alias dothing 'eval `/target/debug/myapp \!*`'


Since all text send to stdout is eval'd by the shell, great care must be taken to control what is printed to stdout. All user-facing messages should go to stderr instead.



To the extent possible under law, Andrew Chin has waived all copyright and related or neighboring rights to setenv. This work is published from: United States.

No runtime deps