我如何“阅读"?while 循环中的变量 [英] How do I "read" a variable on a while loop

查看:27
本文介绍了我如何“阅读"?while 循环中的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 while read line 从变量中读取?

How can I read from variable with while read line?

例如:

the_list=$(..code..)

while read line
do
        echo $line

done < $the_list

使用上面的代码给我错误:

using the code above gives me error:

./copy.sh: line 25: $the_list: ambiguous redirect

推荐答案

你可以写:

while IFS= read -r line
do
    echo "$line"
done <<< "$the_list"

请参阅Bash 参考中的§3.6.7此处的字符串"手动.

(我还冒昧地添加了一些双引号,并将 -rIFS= 添加到 read,以避免过多地处理变量的内容.)

(I've also taken the liberty of adding some double-quotes, and adding -r and IFS= to read, to avoid too much mucking around with the contents of your variables.)

这篇关于我如何“阅读"?while 循环中的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆