用于循环遍历bash中的多行字符串而不迭代每一行

时间:2017-04-20 08:48:46

标签: bash

采用以下示例脚本:

Console.WriteLine(MyCompare(">", 3, 8) ? "Y" : "N");
Console.WriteLine(MyCompare(">=", true, false) ? "Y" : "N");
Console.WriteLine(MyCompare("<>", "a", "b") ? "Y" : "N"); // Pascal syntax
Console.WriteLine(MyCompare("!=", "a", "b") ? "Y" : "N"); // C syntax
Console.WriteLine(MyCompare("==", 3.4, 7.8) ? "Y" : "N");

输出将是:

foo="multi
line"
bar="multi
line"
for X in $foo $bar
do
    echo $X
    echo "break"
done

如何迭代整个变量,而不是每一行?所需的输出是:

multi
break
line
break
...

道歉,因为我确信这必须是重复的,但搜索是不可能的。

1 个答案:

答案 0 :(得分:2)

请参阅Why does my shell script choke on whitespace or other special characters?

$ cat ip.sh
#!/bin/bash

foo="multi
line"
bar="multi
line"
for X in "$foo" "$bar"
do
    echo "$X"
    echo "break"
done

$ ./ip.sh 
multi
line
break
multi
line
break