How to split strings over multiple lines in Bash?

前端 未结 11 1185
[愿得一人]
[愿得一人] 2020-11-29 16:35

How can i split my long string constant over multiple lines?

I realize that you can do this:

echo "continuation \\
lines"
>continuation li         


        
11条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-29 17:35

    Line continuations also can be achieved through clever use of syntax.

    In the case of echo:

    # echo '-n' flag prevents trailing  
    echo -n "This is my one-line statement" ;
    echo -n " that I would like to make."
    This is my one-line statement that I would like to make.
    

    In the case of vars:

    outp="This is my one-line statement" ; 
    outp+=" that I would like to make." ; 
    echo -n "${outp}"
    This is my one-line statement that I would like to make.
    

    Another approach in the case of vars:

    outp="This is my one-line statement" ; 
    outp="${outp} that I would like to make." ; 
    echo -n "${outp}"
    This is my one-line statement that I would like to make.
    

    Voila!

提交回复
热议问题