Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I want my script to read a file containing key/value pairs of environment variables to set, and then to set them.

So far, I have this:

#!/bin/bash

cat $1 | while read kv
do
    key=${kv%=*}
    val=`echo ${kv#*=} | sed 's/^"\|"$//g'`
    export $key="$val"
done

And I want to read a file like this:

XAUTHLOCALHOSTNAME="localhost"
DISPLAY=":0"
XAUTHORITY="/tmp/some-XAuthority"

I only need these variables in scope for the duration of the script, so I don't need to solve the problem of setting a variable in script for the parent scope.

From my testing, I think my problem lies with export $key="$val", so I think I just need a replacement for that line.

share|improve this question
    
a bit off topic but since i got stuck while trying to mimic your script, i'm sharing ... when grabbing a key ... using key=${kv%%=*} is better than key=${kv%=*} because %% and ## differ from % and # for whether the removed part is the shortest or longest removable part –  pulkitsinghal Jun 20 '13 at 14:53
add comment

2 Answers

up vote 8 down vote accepted

export $key=$val should work just fine in bash. I suspect your issue is the pipe (|). All commands in a pipeline are executed in a subshell. In your example, the instance of bash that is running your script will fork off 2 subshells: one for cat $1 and another for the while read .... The variables are assigned and exported in the subshell running the while loop, and then all of them are promptly thrown out when the subshell exits. One way to fix this is to not spawn subshells at all. Instead of a useless use of cat, try redirection instead:

while read ...; do
   ...
done < "$1"

BashFAQ 24 explains this in much greater detail.

An alternate approach is to just source the file, with exports thrown in:

. <(sed '/^export/!s/^/export /' "$1")

The <( ) is process substitution.

As an additional note of caution, since you are reading environment variables from an argument, you must make sure that the argument file $1 is a trusted source so it can't do bad things to your script.

share|improve this answer
    
I used redirection previously; something else must have not worked when I changed the script to use cat. But, the script with redirection works now, thanks! –  palswim Feb 15 '12 at 23:44
add comment

(jw013 has already given a correct answer, but I want to point out a few more issues.)

The right-hand side of a pipeline runs in its own subshell in bash (as well as most other shells, the exceptions being ATT ksh and zsh). So you're setting the environment variables in the subshell that executes the loop, but not in the main shell process.

Here there's a easy fix which is to replace the useless use of cat by a redirection:

while read …; do …; done <"$1"

In general, if you need to preprocess the input, put the loop and the remainder of the script (at least the part that needs the changed variables) inside a block.

grep '^foo_' "$1" | {
  while read …; do …; done
  do_something
}

Note that in general, while read line; do … does not quite iterate over the input lines. See Why is while IFS= read used so often, instead of IFS=; while read..? for an explanation. The correct idiom to iterate over input lines is

while IFS= read -r line; do …

Here, the removal of leading and trailing whitespace doesn't matter since there shouldn't be any given your sample input, but you may need -r to avoid backslash expansion. It's possible that while read line is in fact a correct way to read your input (but I suspect only if the variable values contain no newlines). It's also possible that your input format is ambiguous or more complex to parse. Check what happens if one of the variables' value contains a newline character, a double quote or a backslash.

One point where you are definitely mangling the input is when you extract the value:

val=`echo ${kv#*=} | sed 's/^"\|"$//g'`

First, you need to use double quotes around the variable substitution: echo "${kv#*=}"; otherwise the shell performs word splitting and filename generation (i.e. globbing) on it. Second, echo is not a reliable way to print a string, because some strings that begin with a - are treated as options, and some shells perform backslash expansion on the argument. A reliable and portable way to print a string is printf %s "${kv#*=}". Also, if the value spans multiple lines, the sed program will act on each separately, which is wrong. This is fixable, but there's an easier method, which is to use the shell's string manipulation facilities: val=${kv#*=}; val=${val#\"}; val=${val%\"}.

Assuming that your input is correctly parsed with a plain read, here's a simpler way to parse it, taking advantage of the IFS setting to split each line:

while read name value; do
  value=${value#\"}; value=${value%\"}
  export name="$value"
done <"$1"
share|improve this answer
add comment

Your Answer

 
discard

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.