1

in bash why does this work fine:

$ cat test1.sh
#!/bin/bash
echo "some text" \
"some more text"
$ ./test1.sh
some text some more text

but this fails

$ cat test2.sh
#!/bin/bash
text="some text" \
"some more text"
echo $text
$ ./test2.sh
./test2.sh: line 3: some more text: command not found

I was expecting both test1.sh and test2.sh to do the same thing.

5

Quoting POSIX Shell Command Language,

A <backslash> that is not quoted shall preserve the literal value of the following character, with the exception of a <newline>. If a <newline> follows the <backslash>, the shell shall interpret this as line continuation. The <backslash> and <newline> shall be removed before splitting the input into tokens. Since the escaped <newline> is removed entirely from the input and is not replaced by any white space, it cannot serve as a token separator.

This means that, in your first example, what the shell actually executes is

echo "some text" "some more text"

which is the simple command echo followed by two arguments, concatenated using a space character when printed to standard output.

In your second example, what the shell actually executes is

text="some text" "some more text"
echo $text

where the first line is interpreted as the simple command some more text (a single token, including the space characters) preceded by the variable assignment text="some text"; then, echo $text is executed.

To produce the same result as the first one, your second snippet may be changed into

text="some text "\
"some more text"
echo "$text"

Note, also, the double quotes in echo "$text", needed to prevent the shell from applying word splitting and filename generation to the expansion of $text (it makes no difference with your sample strings, but it would if they contained whitespace character sequences other than a single space and/or globbing characters).

4
  • wow! that's a great answer - i had no idea you could put "\ (without the space between " and \ ) to concatenate a string. – mulllhausen Jun 13 at 10:42
  • beware it doesn't work if there is an indent or space at the start of the second line though - before the quote. – mulllhausen Jun 13 at 10:47
  • 1
    @mulllhausen Yep, whitespace characters before the second quoted string would have the same effect of whitespace characters between the first quoted string and the backslash. – fra-san Jun 13 at 10:49
  • 1
    Also, keep in mind that quotes don't define a string, since everything is already a string in shell. Instead, quotes are simply a way to escape characters in bulk. "foo" and \f\o\o are identical. – chepner Jun 13 at 22:48
2

In test1.sh echo prints all arguments given to it - "some text" is the first argument, and "some more text" is the second argument.

Whereas test2.sh is attempting to concatenate strings and assign to a variable using an incorrect concatenation method.

To break a string across lines simply leave off the closing quote and continue on the next line, like so:

text="some text \
some more text"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.