Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Line continuation + line comment, on the same line? [duplicate]

Tags:

bash

shell

sh

I have a call with a pretty long list of arguments in my shell script:

foo-command \
    --important-argument $ONE \
    --indispensable-flag $ENABLED \
    --an-optional-toggle "will probably be commented out some day" \
    $ARGUMENTS \
    $MORE_ARGUMENTS

Can't I insert comments in this multiline call, can I?

For example, lets say, how can I comment out the --an-optional-toggle line?
How can I add an # in sorted order comment after $ARGUMENTS?

No matter what I try, the octothorp # symbol shadows line continuation backslash \, or vice versa. Any advise?

like image 358
ulidtko Avatar asked Aug 27 '14 14:08

ulidtko


People also ask

How do you know if a line is a continuation line?

The line being continued is a continued line; the succeeding lines are continuation lines. Area A of a continuation line must be blank. If there is no hyphen (-) in the indicator area (column 7) of a line, the last character of the preceding line is assumed to be followed by a space.

What can and can't be continued on a continuation line?

Area A of a continuation line must be blank. If there is no hyphen (-) in the indicator area (column 7) of a line, the last character of the preceding line is assumed to be followed by a space. The following items cannot be continued: Alphanumeric literals containing DBCS characters

Is it possible to add a comment to a continuation line?

So the reference manual explicitly disallows to add a comment in an explicit continuation line. You can't have comments and backslash for line continuation on the same line. You need to use some other strategy.

How do you write a continuation line in a literal?

The continuation of the literal begins with the character immediately following the quotation mark. If an alphanumeric or national literal that is to be continued on the next line has as its last character a quotation mark in column 72, the continuation line must start with two consecutive quotation marks.


3 Answers

Try the backtick comment hack as proposed in an earlier response to the same question.

In your case, this would be:

foo-command \
    --important-argument $ONE \
    --indispensable-flag $ENABLED \
    `#--an-optional-toggle "will probably be commented out some day"` \
    $ARGUMENTS \
    $MORE_ARGUMENTS
like image 153
lxg Avatar answered Nov 08 '22 18:11

lxg


No, but you can store the arguments in an array instead. This allows you to both comment out an individual line as well as include interspersed comments.

args=( --important-argument "$ONE"
       --indispensable-flag "$ENABLED"
       # --an-optional-toggle "will probably be commented out some day"
       $ARGUMENTS  # in sorted order
       $MORE_ARGUMENTS
     )
foo-command "${args[@]}"

Note that you will almost certainly want to make ARGUMENTS and MORE_ARGUMENTS arrays as well, so args would end up looking like

args=( --important-argument "$ONE"
       --indispensable-flag "$ENABLED"
       # --an-optional-toggle "will probably be commented out some day"
       "${ARGUMENTS[@]}"  # in sorted order
       "${MORE_ARGUMENTS[@]}"
     )
like image 24
chepner Avatar answered Nov 08 '22 19:11

chepner


foo-command $(
    # hello
    printf --important-argument $ONE # free as a bird
    printf --indispensable-flag $ENABLED
    # to comment anywhere we wish
    printf --an-optional-toggle "will probably be commented out some day"
    printf $ARGUMENTS
    printf $MORE_ARGUMENTS
)

It's not perfect: echoing -n is hard because echo interprets it; quotes may be disappeared when you would prefer to keep them, etc. In fact as commenters say below, the quoted string will get mangled; maybe you can work around that, but other answers here are better if you have Bash.

like image 43
John Zwinck Avatar answered Nov 08 '22 20:11

John Zwinck