Bash continuation lines

2019-01-08 05:11发布

How do you use bash continuation lines?

I realize that you can do this:

echo "continuation \
lines"
>continuation lines

However, if you have indented code, it doesn't work out so well:

    echo "continuation \
    lines"
>continuation     lines

9条回答
女痞
2楼-- · 2019-01-08 06:01

However, if you have indented code, it doesn't work out so well:

    echo "continuation \
    lines"
>continuation     lines

Try with single quotes and concatenating the strings:

    echo 'continuation' \
    'lines'
>continuation lines

Note: the concatenation includes a whitespace.

查看更多
爷的心禁止访问
3楼-- · 2019-01-08 06:05

You can use bash arrays

$ str_array=("continuation"
             "lines")

then

$ echo "${str_array[*]}"
continuation lines

there is an extra space, because (after bash manual):

If the word is double-quoted, ${name[*]} expands to a single word with the value of each array member separated by the first character of the IFS variable

So set IFS='' to get rid of extra space

$ IFS=''
$ echo "${str_array[*]}"
continuationlines
查看更多
小情绪 Triste *
4楼-- · 2019-01-08 06:07

Depending on what sort of risks you will accept and how well you know and trust the data, you can use simplistic variable interpolation.

$: x="
    this
    is
       variably indented
    stuff
   "
$: echo "$x" # preserves the newlines and spacing

    this
    is
       variably indented
    stuff

$: echo $x # no quotes, stacks it "neatly" with minimal spacing
this is variably indented stuff
查看更多
登录 后发表回答