加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-15 19:35:51 所属栏目:安全 来源:网络整理
导读:读取行时如何读取变量? 例如: the_list=$(..code..)while read linedo echo $linedone $the_list 使用上面的代码给我错误: ./copy.sh: line 25: $the_list: ambiguous redirect 你可以写: while IFS= read -r linedo echo "$line"done "$the_list" 见§3
读取行时如何读取变量?

例如:

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 =来阅读,以避免太多的内容与你的变量的内容混淆。)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读