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?
-
See additional discussion at stackoverflow.com/questions/18599711/…enharmonic– enharmonic2020-05-13 20:04:00 +00:00Commented May 13, 2020 at 20:04
-
"some lines exceeded 80 columns in length" There is no explicit line length limit in bash. If you want to split a line in two, then you can do so using the techniques discussed in this answer, but the reason that's need is because you decided to split the line, not because of any column limitMarkR– MarkR2024-07-01 02:44:04 +00:00Commented Jul 1, 2024 at 2:44
3 Answers
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.
7 Comments
^M. The problem appears to be that the script was given to me by someone that uses windows. A quick dos2unix fixed it :)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
1 Comment
[[-]] condition, but not in others, and I'm trying to figure out the rules.\ 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.