Solved shell scripts and line continuation characters (\)

How do line continuation character work in a shell script?

Would the following be executed as expected?

Code:
sudo pkg install \
    php84 \
    mod_php84 \
    php84-pdo \
    php84-pgsql \
    php84-gd \
    php84-mbstring \
    php84-opcache \
    php84-session \
    php84-xml \
    php84-dom \
    php84-filter \
    php84-json \
    php84-tokenizer \
    php84-zip \
    php84-curl \
    php84-simplexml
 
Would the following be executed as expected?
Yes, it will :)

In fact, you can always make a simple test script to try it out when you are not sure:
/tmp/test
sh:
#!/bin/sh

pkg inst -n php84 \
    mod_php84 \
    php84-session

Code:
$ /tmp/test 
The following 5 package(s) will be affected (of 0 checked):

New packages to be INSTALLED:
        apache24: 2.4.68 [FreeBSD-ports]
        jansson: 2.15.1_1 [FreeBSD-ports]
        mod_php84: 8.4.23_1 [FreeBSD-ports]
        php84: 8.4.23_1 [FreeBSD-ports]
        php84-session: 8.4.23_1 [FreeBSD-ports]

Number of packages to be installed: 5

The process will require 61 MiB more space.
13 MiB to be downloaded.

From pkg-install(8):
Code:
-n, --dry-run
        Dry-run mode.  The list of changes to packages is always printed, but no changes are actually made.

How do line continuation character work in a shell script?
sh(1):
Code:
Backslash
      A backslash preserves the literal meaning of the following character, with the exception of the newline character (‘\n’).  A backslash preceding a newline is treated as a line continuation.
So \ in the end of the line basically means that you're not finished yet, and your 'sentence' will be continued on the next line.
For special shell sequences like || it's not necessary to put a backslash after in the end of line:
sh:
test "${my_var}" = "hello" ||
    echo "This is not what I want" 1>&2
 
Keep in mind that backslash line continuation is specifically a shell thing. There are no requirements for utility input or config files to honor it, and most do not.
 
Back
Top