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

后端 未结 13 919
北恋
北恋 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:30

    I find the -e flag elegant and straight forward

    bash$ STR="Hello\nWorld"
    
    bash$ echo -e $STR
    Hello
    World
    

    If the string is the output of another command, I just use quotes

    indexes_diff=$(git diff index.yaml)
    echo "$indexes_diff"
    

提交回复
热议问题