这看起来很复杂.我需要一个正则表达式来从Bash shell脚本中删除注释.
请记住$#,${#foo},string="this # string",string='that # string',${foo#bar},${foo##baar},和
string="really complex args=$# ${applejack##"jack"} $(echo "$#, again")"; `echo this is a ${#nasty[*]} example`
Run Code Online (Sandbox Code Playgroud)
是应该都是有效的shell表达式不被剥离.
编辑: 请注意:
# This is a comment in bash
# But so is this
echo "foo bar" # This is also a comment
Run Code Online (Sandbox Code Playgroud)
编辑: 请注意,可能被误解为评论的行可能藏在HEREDOCs内,但由于它是多行的,我可以在不处理/解释它的情况下生活:
cat<<EOF>>out.txt
This is just a heredoc
# This line looks like a comment, but it isn't
EOF
Run Code Online (Sandbox Code Playgroud)
你不能用正则表达式做到这一点.
echo ${baz/${foo/${foo/#bar/foo}/bar}/qux}
Run Code Online (Sandbox Code Playgroud)
您需要匹配嵌套大括号.正则表达式不能这样做,除非你愿意考虑PCRE的"正则表达式",在这种情况下,在Perl中编写解析器会更简单.