Answers:
这是可能的。尝试这个:
grep 'hello' <<< 'hello world'
您还可以传递包含字符串的变量:
str='hello world'
grep 'hello' <<< $str
grep
没有选项将其命令行参数解释为要搜索的文本。grep
字符串的通常方法是将字符串传递到grep
的标准输入中:
$ echo 'There once was a man from Nantucket
Who kept all his cash in a bucket.
But his daughter, named Nan,
Ran away with a man
And as for the bucket, Nantucket.' | grep -i nan
There once was a man from Nantucket
But his daughter, named Nan,
And as for the bucket, Nantucket.
$
如您在此处看到的,您可以echo
包含多行文本的字符串。如果愿意,您甚至可以交互方式将其键入到外壳中。
如果这不能满足您的需求,也许您可以解释为什么管道不是可接受的解决方案?
用管道输送到 grep
为什么不只是:
echo 'hello world' | grep 'hello'
另请参阅:https : //stackoverflow.com/questions/2106526/how-can-i-grep-complex-strings-in-variables
grep
?所以也许类似的东西printf "various\ntext to grep here" | grep "text"
会产生“这里的文本到grep”