0

Usually, I type long command on separate lines like this:

myapp -a foooooooo1          \
      -b foooo2              \
      -c foooooooooooooooo3  \
      -d foooooooo4          \
          bar

But I want to change this to the following:

myapp  -a foooooooo1  
    \  -b foooo2
    \  -c foooooooooooooooo3
    \  -d foooooooo4
          bar

I want to do this because when I change the first piece of code, I need to manually adjust the backslashes so that they line up correctly under each other. With the second piece of code, the backslashes would not move if I changed one of the arguments.

7
  • I think you can't change it. You have to use `` to inform bash that there is continuation in next line. This method is popular and used in others shells and languages.
    – furas
    May 2, 2021 at 5:37
  • The backslash is used to remove the special meaning of a character. A backslash at the end of the line, like in the first code block, removes the special meaning of the newline character. A backslash in front of a space removes its meaning of a separator. The second code block attempts to execute myapp -a fooooooo1, then the next command -b foooo2 etc. Since there is no command named -b, this will fail. May 2, 2021 at 5:44
  • Hi @Mr-R : i want to keep all backslash \ in some place , if it's in begin of line it's not moving after any modification, but if in the end , you must change after any modification , also i hate tab , i'm using just space
    – nextloop
    May 2, 2021 at 5:58
  • 1
    @AyoubElMhamdi The backslash at the end of the line tells the shell that the command is continuing on the next line. A backslash at the start of the line, like you propose, has no meaning.
    – Kusalananda
    May 2, 2021 at 6:01
  • @berndbausch yes, it's true
    – nextloop
    May 2, 2021 at 6:03

1 Answer 1

3

The function of an unquoted backslash in a command in the shell is to escape the next character. Another way to say that is that the backslash removes the special meaning of the next character, if that character has a special meaning.

The newline is a command terminator, just like ; and a few other characters. The backslash removes that function from the newline, so that the command may continue on the next line. This is, by the way, why you can't even have a space character after the backslash at the end of a line that needs to be continued, as the backslash would then not escape the actual newline.

Your proposed code would not be parsed correctly by the shell. Instead it would try to run each line as individual commands.

Another approach that may possibly be useful under some circumstances, if your shell has support for arrays (as bash, for example, has), is to store the command in an array like so:

mycommand=(
    myapp -a 'foo foo'  
          -b 'bar bar'
          -c 'baz baz'
          -d 'qux qux'
              bar
)

The newlines in this array assignment just functions as separators between the array elements, just like the spaces do. Elements that needs spaces embedded needs to be quoted, as shown in the example here (and just like they would have needed to be quoted on the command line in any case).

To run that command, you would use

"${mycommand[@]}"

Note that the double quotes are essential. They ensure that each element of the array is individually quoted.

Alternatively, use an array only for the arguments:

myapp_args=(
    -a 'foo foo'  
    -b 'bar bar'
    -c 'baz baz'
    -d 'qux qux'
        bar
)

and then:

myapp "${myapp_args[@]}"
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .