grep命令,值包含空格

发布于 2025-01-24 22:14:12 字数 614 浏览 2 评论 0原文

我有一个文件mail.txt,其中包含以下内容:

John Doe [email protected]
Mary Doe [email protected]

如果我在控制台中键入命令:

grep -ia 'John Doe' mail.txt 

我会收到结果,但是如果我想在脚本中写下它,例如:

var="'John Doe'"
mail=mail.txt
result="`grep -ia $var $mail`"

我收到错误:grep:grep:grep: doe':没有这样的文件或目录。如何解决?

I have a file mail.txt with the following contents:

John Doe [email protected]
Mary Doe [email protected]

if I type the command in the console :

grep -ia 'John Doe' mail.txt 

I receive a result, but if I want to write it in a script, for example:

var="'John Doe'"
mail=mail.txt
result="`grep -ia $var $mail`"

I receive the error: grep: Doe': No such file or directory. How can this be solved?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

舞袖。长 2025-01-31 22:14:12

您只需要使用双引号即可分配变量。
因此,在定义命令时,您可以再次使用双引号。
看起来像这样:

var="John Doe"
mail=mail.txt
grep -ia "$var" $mail

var="John Doe"
mail=mail.txt
result=`grep -ia "$var" $mail`
echo $result

You just need to use double quotes to assign the variable.
So you can use double quotes again when defining the command.
could look like this:

var="John Doe"
mail=mail.txt
grep -ia "$var" $mail

or

var="John Doe"
mail=mail.txt
result=`grep -ia "$var" $mail`
echo $result
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文