Shell Problems passing strings containing a space to env

I am writing a rc script to start a node application. I am trying to use /usr/bin/env to set environment variables I have put in a file. The Problem I have is the variable that was loaded in the script is not being treated the same way as if I was using literals. if I call /usr/bin/env TEST="TEST TEST TEST" /usr/local/bin/zsh when I look call /usr/bin/env inside new zsh shell it will show the saved envronment variable as TEST=TEST TEST TEST. If the same TEST="TEST TEST TEST" line were read from a file and stored in a variable only TEST="TEST would be passed and the program would not be called. If I change the file to remove the spaces zsh would be called but the TEST variable would have the value "TESTTESTTEST" (including the quotes.) I seems like I am missing something basic here but how can I have the sh varable pass a string to env as if it were typed?
 
Inside a script /usr/bin/env "TEST=TEST TEST TEST" /usr/local/bin/zsh -c env works for me (look at the position o fthe doublequotes).
export TEST="TEST TEST TEST" and then /usr/local/bin/zsh -c env works, too.
(-c env just used for testing)
 
I am trying to use /usr/bin/env to set environment variables I have put in a file.
Not sure exectly what you mean but I think what are trying to achieve is something like the following.

First, taking that you're trying to call from inside an rc script, that assumes you're running the standard sh(1) when you execute:
/usr/bin/env TEST="TEST TEST TEST" /usr/local/bin/zsh
or at least the equivalent you want. You haven't shown what exactly you have tried and how that failed. As an example, I'll take an ordinary sh script file test.sh to represent the relevant stuff.
Demonstration run:
Rich (BB code):
# ps -p $$
  PID TT  STAT    TIME COMMAND
93299  1  S    0:00.01 sh
# cat test.sh
#!/bin/sh
# testing environment value passing to zsh
T1='TEST TEST'
T2='bla blup'
/usr/bin/env E1="${T1}" E2="${T2}" C1="${T1}--${T2}" /usr/local/bin/zsh
# env | grep -E 'E1|E2|C1'
# ./test.sh
tm1# ps -p $$
  PID TT  STAT    TIME COMMAND
93305  1  S    0:00.02 /usr/local/bin/zsh
tm1# env | grep -E 'E1|E2|C1'
C1=TEST TEST--bla blup
E2=bla blup
E1=TEST TEST
tm1#
Before calling the zsh(1), you can see that when running sh(1) (line # ps -p $$ tells what shell you are currently running) the enviroment variables E1, E2 and C1 are not in the sh(1) environment. After invoking the zsh(1) the values for E1, E2 and C1 have been successfully passed and are part of the zsh(1) environment.

In the script line /usr/bin/env E1="${T1}" E2="${T2}" C1="${T1}-${T2}" /usr/local/bin/zsh, the use of "${<variable>}" is a good way to reference a variable in a script, it also enables the combination of variables and strings together. More information about that: Using curly braces with variables
 
Back
Top