setting environment variables with space

Hi,

I'm having problems setting environment variable that has space value. Below is my shell script.

export LINE=$@
TO=`echo $LINE | awk '{print $1}'`
CC=`echo $LINE | awk '{print $2}'`
BC=`echo $LINE | awk '{print $3}'`
echo "TO=$TO"
echo "CC=$CC"
echo "BC=$BC"
echo "1=$1"
echo "2=$2"
echo "3=$3"

Then the output is:

$ ksh test2.sh "1" "2.3, 2.6" "3"
TO=1
CC=2.3,
BC=2.6
1=1
2=2.3, 2.6
3=3
$

I'm in a situation where I can't use $1 $2 $3 which is why because in the program that I'm working on, $1/$@ = "1" "2.3, 2.6" "3". I just parse them by doing the following:

set -- $@
TO=`echo $@ | awk '{print $1}'`
CC=`echo $@ | awk '{print $2}'`
BC=`echo $@ | awk '{print $3}'`

I'd appreciate your help.

$ uname -a
SunOS server 5.10 Generic_142900-13 sun4u sparc SUNW,SPARC-Enterprise

Edit:

I tried testing using the following:

$ ksh test2.sh '"1" "2.3, 2.6" "3"'
TO="1"
CC="2.3,
BC=2.6"
1="1" "2.3, 2.6" "3"
2=
3=

I need to get CC to have 2.3, 2.6. How can I do this?

Edit:
The first line was missing in my code. Added

export LINE=$@

Thanks.

Why do you need to parse them with set?
Can't you just use $1,$2,$3?

i.e

Inside test2.sh

TO="$1"
CC="$2"
BC="$3"

because $1 would display the whole thing. "1" "2.3, 2.6" "3" as seen in the output in my first post.

Ok..Sorry I didn't see your explanation clearly.
in that case you could change the IFS to '"' then set.

something like..

IFS='"'
set -- $@
echo "TO=$1"
echo "CC=$2"
echo "BCC=$3"

Alternatively, you could remove the spaces after "," ( if its allowed )

LINE=$(echo "$@" | sed 's/, /,/g')
set -- $LINE
echo $1
echo $2
echo $3
1 Like

Thank you so much! The later part works for me. :smiley:

Hi,

I know this is a bit old thread but I stumbled upon another issue with this. I have basically a similar requirement but this time, I can't remove spaces in between.

Example:

$ LINE='"ROGERS, STEVE" "27-JUL-2011" "Open" "Now Showing"'
$ echo "$LINE"
"ROGERS, STEVE" "27-JUL-2011" "Open" "Now Showing"
$ IFS='"'
$ set -- $LINE
$ echo $1

$ echo $2
ROGERS, STEVE
$ echo $3

$ echo $4
27-JUL-2011
$ echo $5

$ echo $6
Open
$ echo $7

$ echo $8
Now Showing
$

I noticed that the values do not go to $1 $2 $3 $4 as expected. How do I solve this?

Thanks.