简体   繁体   English

如何在shell脚本中使用带引号的grep?

[英]How to use grep with quotes inside a shell script?

I am trying to use the following pattern inside a shell script eg. 我正在尝试在shell脚本中使用以下模式。 I want to use the following: 我要使用以下内容:

grep ^"#" $fn | grep -v NM$ > $op

but inside bash script. 但在bash脚本中。 The problem is normally bash considers everything after the "#" as a comment. 问题通常是bash将“#”之后的所有内容都视为注释。 If i use 如果我用

grep ^"\#" $fn

I think it changes the meaning. 我认为这改变了含义。 I am a newbie. 我是新手。

Any help will be appreciated. 任何帮助将不胜感激。

There are multiple possibilities: 有多种可能性:

grep '^#' $fn

or 要么

grep "^#" $fn

or 要么

grep ^\# $fn

Here is a little example which I saved as foo.sh 这是我保存为foo.sh一个小示例

echo "#"
echo '#'  # this is a comment
echo '\#'

when run as sh foo.sh this is what is printed out 当以sh foo.sh运行时,这就是打印出来的内容

#
#
\#

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM