173

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 the line continues on the next line?

dcharles
  • 4,822
  • 2
  • 32
  • 29
Open the way
  • 26,225
  • 51
  • 142
  • 196
  • See additional discussion at https://stackoverflow.com/questions/18599711/how-can-i-split-a-shell-command-over-multiple-lines-when-using-an-if-statement – enharmonic May 13 '20 at 20:04

3 Answers3

227

The character is a backslash \

From the bash manual:

The backslash character ‘\’ may be used to remove any special meaning for the next character read and for line continuation.

Paolo
  • 20,112
  • 21
  • 72
  • 113
Guillaume
  • 18,494
  • 8
  • 53
  • 74
  • thanks. and what is the usual limit in columns of a bash script? – Open the way Oct 06 '10 at 10:06
  • bash has no interesting column limit; for clarity you should try to limit to 70-80 chars per column. – Habbie Oct 06 '10 at 10:07
  • This doesn't seem to be working for me? Is it contingent on other factors? bash version? – RyanM Oct 19 '13 at 22:52
  • 20
    @RyanM The backslash has to be the very last character before the end of line character. Are you SURE you don't have any whitespace after the \ ? – George Oct 19 '13 at 23:31
  • 10
    @George Yeah. A little more fiddling spit out an error with a `^M`. The problem appears to be that the script was given to me by someone that uses windows. A quick `dos2unix` fixed it :) – RyanM Oct 20 '13 at 00:04
  • 3
    @George your comment just saved me, I had a space after the \ and I couldn't figure out what was wrong. Cheers! – ffledgling Dec 16 '13 at 14:39
  • We could replace the link in the answer with https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#index-read (I added an anchor to the specific section) – Sylvain Lesage Apr 29 '22 at 14:48
73

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
chepner
  • 497,756
  • 71
  • 530
  • 681
  • Is there a formal list of such situations, where a command is implicitly continued onto the next line? It seems to me that I could leave the backslash out in certain circumstances within a `[[`-`]]` condition, but not in others, and I'm trying to figure out the rules. – Menachem Jan 05 '18 at 04:04
31

\ does the job. @Guillaume's answer and @George's comment clearly answer this question. Here I explains why The backslash has to be the very last character before the end of line character. Consider this command:

   mysql -uroot \
   -hlocalhost      

If there is a space after \, the line continuation will not work. The reason is that \ removes the special meaning for the next character which is a space not the invisible line feed character. The line feed character is after the space not \ in this example.

Jingguo Yao
  • 7,320
  • 6
  • 50
  • 63
  • 3
    Thank you for pointing out the "very last character" part. I was getting a bit frustrated tracking exactly this bug. – Jason Nov 01 '16 at 04:51