chexum (chexum) wrote,

  • Mood:

Using env or how not to pollute the environment

rc(1) is my preferred shell for many kinds of shell scripting tasks. Sometimes even interactively(2), but zsh is a clear winner there. It is small, efficient, clean. It might look dumb after using zsh/bash for a while, as the kind of advanced string manipulation one gets used to in those shells is not existing here. The upside? This forces me to think about the problem, and decide what tool is best for solving it.

As an example, for removing elements from PATH, I used sed in this script. Further, as rc does not have the concept of "export", each user variable ends up in the environment of the executed programs, so one needs to clean up the environment itself for extra correctness. This is a bit difficult if I need to use a given variable for naming the program to be run. So there is a need for running "env" as shown below.

# unwrap: used in shadowed copies of commands
# as in ~/bin/ssh: unwrap ssh $myargs
if (~ $#* 0) { exit 1; }
PATH=`{echo -n :$PATH:|sed 's,/home/[^:]\+/'^$USER^'/bin:,,g;s/^://;s/:$//'}
exec env -u cmd -- $cmd $*
  1. rlwrap is a very nice little program to add history and line editing to otherwise dumb interactive programs.
  2. rc was here, but it's gone stale recently, the reportedly newer page isn't working either now.
Tags: env, rc, sed, shell
  • Post a new comment


    default userpic

    Your IP address will be recorded