如何在 shell 脚本的变量中存储有空格的文件名

How do I store file names that have spaces in a variable in shell script

您好,我有一个名为 MYFILES.txt

的文本文件

当我 运行 grep --null ".txt" MYFILES.txt 它会正确显示带有 space 字符的文件名

grep --null ".txt" MYFILES.txt

my file 1.txt
my file 2.txt
my file 3.txt
my file 4.txt

但是在 shell 脚本中,如果我有这样的东西,那么 space 字符将被排除: 我需要将我从文本中 grep 得到的每个文件名存储在 FILE 变量中,其中包含 space 字符

for FILE in $( grep --null ".txt" MYFILES.txt)
do
   echo "${FILE}"
done

my
file
1.txt
my
file
2.txt
my
file
3.txt
my
file
4.txt

如果文件名不包含换行符

grep .txt MYFILES.txt | while IFS= read -r file ; do
    echo "$file"
done