在 bash 中读取时动态找出参数的数量

dynamically find out num of args for while read in bash

假设在 file1 我每行的字数是随机的, while read 有没有办法知道每行有多少行 应该阅读,这里我硬编码了三个:

while read w1 w2 w3
do
    # grep [some regex] file2
done <<< $(awk '{print [=11=]}' file1)

但想知道是否有可能有这样的东西:

while read [*words] #as many as they are for the current line I am getting from awk
    ...
    ...
done <<< $(awk '{print [=12=]}' file1)

这是否可能,无需在单独的文件中编写更复杂的脚本, 但是 运行 直接来自终端?

显而易见的方法:

while read words; do
    for word in $words; do ...; done
done < file1

将它们读入数组。之后您可以使用普通数组技术。

while read -a words
   ...
done < file1