在 find/grep 搜索结果中保留换行符

pfm*_*e60 5 bash grep find newlines

我正在使用以下脚本递归搜索从其他文件调用的 php 脚本的出现。

find . -exec grep -Hn $1 {} \; | grep -v ^Binary;
Run Code Online (Sandbox Code Playgroud)

效果很好!现在,我需要返回的结果来确定下一步要采取的行动。

r=$(find . -exec grep -Hn $str {} \; | grep -v ^Binary;)

 if [ -z "$r" ];
    then
          Do this
    else
          Do something else
    fi
Run Code Online (Sandbox Code Playgroud)

问题: find 脚本通过它自己返回结果,每个结果都在一个新行上。

./path/to/file.php
./path/to/another_file.php
./path/to/third_file.php
Run Code Online (Sandbox Code Playgroud)

但是,将输出分配给 $r 变量时,不会保留换行符,结果打印在一行上,因此难以阅读。

./path/to/file.php ./path/to/file.php ./path/to/third_file.php
Run Code Online (Sandbox Code Playgroud)

如何在将输出分配给变量时保留换行符?

gle*_*man 9

你没有展示你用什么做的$r,但我敢打赌

echo $r
Run Code Online (Sandbox Code Playgroud)

您需要将变量括在双引号中以保留换行符

echo "$r"
Run Code Online (Sandbox Code Playgroud)

未加引号,变量受分词影响,其中任何空格1(包括换行符)序列都被单个空格1替换

1 : 默认情况下,取决于$IFS(默认:空格、制表符、换行符)的内容