#shell #batch #bash #zsh #ksh

setenv

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

#6 in #batch

Download history 45/week @ 2020-10-30 28/week @ 2020-11-06 37/week @ 2020-11-13 37/week @ 2020-11-20 22/week @ 2020-11-27 37/week @ 2020-12-04 36/week @ 2020-12-11 19/week @ 2020-12-18 102/week @ 2020-12-25 97/week @ 2021-01-01 82/week @ 2021-01-08 56/week @ 2021-01-15 115/week @ 2021-01-22 44/week @ 2021-01-29 68/week @ 2021-02-05 96/week @ 2021-02-12

95 downloads per month

CC0 license

11KB
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.

Install

Add the following to your Cargo.toml file:

[dependencies]
setenv = "0.1"

Usage

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.

Examples

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

Windows:

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

Bash:

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

Ksh:

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

Zsh:

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

Tcsh:

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

Notes

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.

License

http://creativecommons.org/publicdomain/zero/1.0/

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