bash – 如何在while循环中“读取”变量

前端之家收集整理的这篇文章主要介绍了bash – 如何在while循环中“读取”变量前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
读取行时如何读取变量?

例如:

the_list=$(..code..)

while read line
do
        echo $line

done < $the_list

使用上面的代码给我错误

./copy.sh: line 25: $the_list: ambiguous redirect
你可以写:
while IFS= read -r line
do
    echo "$line"
done <<< "$the_list"

§3.6.7 “Here Strings” in the Bash Reference Manual

(我也有机会添加一些双引号,并添加-r和IFS =来阅读,以避免太多的内容与你的变量的内容混淆。)

原文链接:https://www.f2er.com/bash/388308.html

猜你在找的Bash相关文章