本文翻译自:How to put a line comment for a multi-line command [duplicate]
This question already has an answer here: 这个问题已经在这里有了答案:
- Commenting in a Bash script 7 answers 在Bash脚本中评论 7个答案
I know how to write a multi-line command in a Bash script, but how can I add a comment for each line in a multiline command? 我知道如何在Bash脚本中编写多行命令,但是如何在多行命令中为每行添加注释?
CommandName InputFiles \ # This is the comment for the 1st line
--option1 arg1 \ # This is the comment for the 2nd line
--option2 arg2 # This is the comment for the 3nd line
But unfortunately, the comment after continuation character \\
will break the command. 但是不幸的是,连续字符\\
后的注释将破坏命令。
#1楼
参考:https://stackoom.com/question/dxGp/如何为多行命令添加行注释-重复
#2楼
This is how I do it. 这就是我的方法。 Essentially by using Bash's backtick command substitution one can place these comments anywhere along a long command line even if it is split across lines. 本质上,通过使用Bash的backtick 命令替换,即使将注释分成几行,也可以将这些注释沿长命令行放置在任何位置。 I have put the echo command in front of your example so that you can execute the example and see how it works: 我将echo命令放在您的示例前面,以便您可以执行示例并查看其工作方式:
echo CommandName InputFiles `#1st comment` \
--option1 arg1 `#2nd comment` \
--option2 arg2 `#3rd comment`
Another example where you can put multiple comments at different points on one line: 另一个示例,您可以在一条线上的不同点放置多个注释:
some_cmd --opt1 `#1st comment` --opt2 `#2nd comment` --opt3 `#3rd comment`
#3楼
Based on pjh's comment to another answer to this question , replacing IFS
with a variable known to contain no non-whitespace characters. 根据pjh 对这个问题的另一个答案的评论,用已知不包含非空白字符的变量替换IFS
。
comment=
who ${comment# This is the command} \
-u ${comment# This is the argument}
#4楼
You could store the arguments in an array: 您可以将参数存储在数组中:
args=(InputFiles # This is the comment for the 1st line
# You can have whole lines of comments in between, useful for:
#--deprecated-option # This isn't use any more
--option1 arg1 # This is the comment for the 2nd line
# And even blank lines in between for readability
--option2 arg2 # This is the comment for the 3nd line
)
CommandName "${args[@]}"
However I think this looks a bit hackish if it is only for the purpose of allowing comments for each argument. 但是,我认为这仅是为了允许每个参数都带有注释而已,这似乎有些骇人听闻。 Therefore I'd just rewrite the comment so that it refers the the individual arguments, and put it above the whole command. 因此,我只是重写注释,以便它引用各个参数,并将其置于整个命令之上。
#5楼
I'm afraid that, in general, you can't do what you're asking for. 恐怕总的来说,您做不到您想要的。 The best you can do is a comment on the lines before the command, or one single comment at the end of the command line, or a comment after the command. 最好的办法是在命令前的行上添加注释,或在命令行末尾添加一个注释,或在命令后添加注释。
You can't manage to intersperse comments inside a command this way. 您无法通过这种方式在命令中散布注释。 The \\
s express an intent to merge lines, so for all intents and purposes you're trying to intersperse comments in a single line, which doesn't work anyway because a \\
has to be at the end of the line to have that effect. \\
表示合并行的意图,因此出于所有意图和目的,您尝试将注释插入单个行中,但是无论如何都行不通,因为\\
必须位于行的末尾才能产生效果。