我该如何与QUOT;读"在while循环变量 [英] How do I "read" a variable on a while loop

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

问题描述

如何从变量读取同时读取行

例如:

  the_list = $(.. code ..)而读线

        回声$线完成< $ the_list

使用上述code给我的错误:

  ./ copy.sh:第25行:$ the_list:暧昧重定向


解决方案

您可以这样写:

 而IFS =读-r线

    回声$线
完成<<< $ the_list

请参阅和教派;在击参考3.6.7这里琴弦手册

(我也考虑加入一些双引号,并加入 -r IFS = 到,以避免过多的与你的变量的内容碴左右)

How can I read from variable with while read line?

For example:

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

解决方案

You can write:

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

See §3.6.7 "Here Strings" in the Bash Reference Manual.

(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.)

这篇关于我该如何与QUOT;读&QUOT;在while循环变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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