How to output a multiline string in Bash?

后端 未结 9 1628
忘了有多久
忘了有多久 2021-01-29 18:14

How can I output a multipline string in Bash without using multiple echo calls like so:

echo \"usage: up [--level | -n ][--help][--version         


        
相关标签:
9条回答
  • 2021-01-29 18:53

    Use -e option, then you can print new line character with \n in the string.

    Sample (but not sure whether a good one or not)

    The fun thing is that -e option is not documented in MacOS's man page while still usable. It is documented in the man page of Linux.

    0 讨论(0)
  • 2021-01-29 18:54

    Use the -e argument and the escape character \n:

    echo -e "This will generate a next line \nThis new line is the result"
    
    0 讨论(0)
  • 2021-01-29 18:56

    If you use the solution from @jorge and find that everything is on the same line, make sure you enclose the variable in quotes:

    echo $__usage
    

    will print everything on one line whereas

    echo "$__usage"
    

    will retain the newlines.

    0 讨论(0)
提交回复
热议问题