How to split strings over multiple lines in Bash?

前端 未结 11 1187
[愿得一人]
[愿得一人] 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条回答
  •  忘掉有多难
    2020-11-29 17:40

    In certain scenarios utilizing Bash's concatenation ability might be appropriate.

    Example:

    temp='this string is very long '
    temp+='so I will separate it onto multiple lines'
    echo $temp
    this string is very long so I will separate it onto multiple lines
    

    From the PARAMETERS section of the Bash Man page:

    name=[value]...

    ...In the context where an assignment statement is assigning a value to a shell variable or array index, the += operator can be used to append to or add to the variable's previous value. When += is applied to a variable for which the integer attribute has been set, value is evaluated as an arithmetic expression and added to the variable's current value, which is also evaluated. When += is applied to an array variable using compound assignment (see Arrays below), the variable's value is not unset (as it is when using =), and new values are appended to the array beginning at one greater than the array's maximum index (for indexed arrays) or added as additional key-value pairs in an associative array. When applied to a string-valued variable, value is expanded and appended to the variable's value.

提交回复
热议问题