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?
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.
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
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.
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.
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
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[@]}"
)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With