Pet*_*Lee 303 bash shell comments
我知道如何在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
Run Code Online (Sandbox Code Playgroud)
但不幸的是,继续角色之后的评论\将打破命令.
Mar*_*agh 573
我就是这样做的.基本上通过使用Bash的反引号命令替换,可以将这些注释放在长命令行的任何位置,即使它是跨行分割的.我已将echo命令放在您的示例前面,以便您可以执行该示例并查看其工作原理:
echo CommandName InputFiles `#1st comment` \
--option1 arg1 `#2nd comment` \
--option2 arg2 `#3rd comment`
Run Code Online (Sandbox Code Playgroud)
另一个例子,您可以在一行的不同点放置多个注释:
some_cmd --opt1 `#1st comment` --opt2 `#2nd comment` --opt3 `#3rd comment`
Run Code Online (Sandbox Code Playgroud)
Phi*_*ipp 73
您可以将参数存储在数组中:
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[@]}"
Run Code Online (Sandbox Code Playgroud)
但是,我认为如果只是为了允许每个参数的注释,这看起来有点hackish.因此,我只是重写注释,以便引用各个参数,并将其置于整个命令之上.
Per*_*rry 63
我担心,一般来说,你不能做你所要求的.您可以做的最好是对命令前面的行进行注释,或者在命令行末尾添加一条注释,或者在命令后注释.
您无法以这种方式在命令中散布注释.在\小号表达的意图,合并线路,所以对于所有意图和目的你想在单行线,不反正工作,因为一来点缀评论\必须是在该行的末尾有这种效果.
che*_*ner 16
基于pjh对此问题的另一个答案的评论,替换IFS为已知不包含非空白字符的变量.
comment=
who ${comment# This is the command} \
-u ${comment# This is the argument}
Run Code Online (Sandbox Code Playgroud)