Loop inside “heredoc” in shell scripting

后端 未结 3 1122
伪装坚强ぢ
伪装坚强ぢ 2020-12-19 10:25

I need to execute series of commands inside an interactive program/utility with parameterized values. Is there a way to loop inside heredoc ? Like below .. Not sure if

3条回答
  •  醉话见心
    2020-12-19 11:09

    Yes, this is tricky and can be confusing! You have to modify your codes as follow.

    #!/bin/sh
    list="OBJECT1 OBJECT2 OBJECT3"
    utilityExecutable << EOF
      list="$list"
      for i in \$list ; do
        utilityCommand \$i
      done
    EOF
    

    This is because heredoc uses its own variables, which are completely separate from the shell. When you are inside heredoc, you have to use and modify heredoc's own variables. So the \$ is needed to reference heredoc's own variables instead of shell variables when inside heredoc.

提交回复
热议问题