Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am using MSYS and have a file vars.txt with variable, value keys like:

ALLUSERSPROFILE C:/Documents and Settings/All Users

I want to read this in and set environment variables up. I have a bash script setenv:

while read var value;
  echo "performing export $var=$value"
  export $var='$value'

and I call it with

cat vars.txt | source setenv

However in my environment the variables are not set. I also tried making it into a function but no joy. Anybody here know what I'm doign wrong? Thanks.

share|improve this question
setenv is a poor name for your script, since it's the name of a builtin in several shells. – Daenyth Sep 17 '10 at 18:43
@Daenyth Thanks - yeah it felt kind of wrong at the time but bash doesn't seem to have it. mapenv is a better name anyways ;) – danio Sep 20 '10 at 8:30
up vote 6 down vote accepted

The pipe sets up a subshell. When the subshell exits, the variables are lost.

Try this:

source setenv < vars.txt

Also your single quotes may prevent the expansion of the variable (I don't know if this is true in MSYS). Try changing the export line to this:

export $var="$value"

You can use declare instead of export if the variables don't need to be exported.

share|improve this answer
So I have to use temp files? (vars.txt was just for testing) I have a function that generates vars.txt. It works OK with: generate_envmap > /tmp/envmap.txt; apply_env < /tmp/envmap.txt. Would be nicer to do generate_envmap | apply_env. – danio Sep 20 '10 at 8:50
@danio: I just based my answer on your example. You said you "have a file". I didn't know that you didn't already have the file. You can use process substitution in Bash: source apply_env < <(generate_envmap) – Dennis Williamson Sep 20 '10 at 13:14
Thanks that seems to be what I want but the MSYS version of bash doesn't seem to have implemented process substitution :-( – danio Sep 21 '10 at 8:21
@danio: Try using a here string and command substitution: source apply_env <<< $(generate_envmap) – Dennis Williamson Sep 21 '10 at 10:12
Thanks for that but doesn't seem to do anything. No errors, but no effect either. The temp file solution is a little ugly but fine. – danio Sep 21 '10 at 11:31

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.