How to tell bash that the line continues on the next line

后端 未结 3 2142
灰色年华
灰色年华 2020-12-02 14:23

In a bash script I got from another programmer, some lines exceeded 80 columns in length. What is the character or thing to be added to the line in order to indicate that th

3条回答
  •  旧巷少年郎
    2020-12-02 14:44

    In general, you can use a backslash at the end of a line in order for the command to continue on to the next line. However, there are cases where commands are implicitly continued, namely when the line ends with a token than cannot legally terminate a command. In that case, the shell knows that more is coming, and the backslash can be omitted. Some examples:

    # In general
    $ echo "foo" \
    > "bar"
    foo bar
    
    # Pipes
    $ echo foo |
    > cat
    foo
    
    # && and ||
    $ echo foo &&
    > echo bar
    foo
    bar
    $ false ||
    > echo bar
    bar
    

    Different, but related, is the implicit continuation inside quotes. In this case, without a backslash, you are simply adding a newline to the string.

    $ x="foo
    > bar"
    $ echo "$x"
    foo
    bar
    

    With a backslash, you are again splitting the logical line into multiple logical lines.

    $ x="foo\
    > bar"
    $ echo "$x"
    foobar
    

提交回复
热议问题