How can I have a newline in a string in sh?

后端 未结 13 760
北恋
北恋 2020-11-22 17:17

This

STR=\"Hello\\nWorld\"
echo $STR

produces as output

Hello\\nWorld

instead of

Hello
Wo         


        
13条回答
  •  清酒与你
    2020-11-22 17:29

    I wasn't really happy with any of the options here. This is what worked for me.

    str=$(printf "%s" "first line")
    str=$(printf "$str\n%s" "another line")
    str=$(printf "$str\n%s" "and another line")
    

提交回复
热议问题