作为列表的变量

A variable as a list

我在 Solaris 2.10 上使用 ksh "Version M-11/16/88i"。

我对 "for" 中的列表有一些问题。

第一个例子如我所愿:

#!/bin/ksh
for i in ONE "one is 1" TWO "two is 2" THREE three
do
echo $i
done

这个输出是:

> ./mytest.ksh 
ONE
one is 1
TWO
two is 2
THREE
three
> 

但是,如果我将代码更改为如下内容:

#!/bin/ksh
MYVAR="ONE \"one is 1\" TWO \"two is 2\" THREE three"
echo "MYVAR=$MYVAR"
for i in $MYVAR
do
echo $i
done

则输出为:

> ./mytest.ksh 
MYVAR=ONE "one is 1" TWO "two is 2" THREE three
ONE
"one
is
1"
TWO
"two
is
2"
THREE
three
>

如何将值保存在变量 MYVAR 中,以便循环 FOR 正确获取它? 谢谢

尝试在呼应卢克的同时引用:

MYVAR="ONE \"one is 1\" TWO \"two is 2\" THREE three"
echo "MYVAR=$MYVAR"
for i in "$MYVAR"
         ^      ^
do
    echo $i
done    
Output:
MYVAR=ONE "one is 1" TWO "two is 2" THREE three                                                                        
ONE "one is 1" TWO "two is 2" THREE three                                                                                                         

这里有一个方法:

#!/bin/sh
MYVAR="ONE \"one is 1\", TWO \"two is 2\", THREE three"
export IFS=",";
echo "MYVAR=$MYVAR"
for i in $MYVAR
do
echo $i
done