如何将文本文件数据存储到 bash 脚本变量中?

How do you store text file data into bash script variables?

我有 bash 脚本需要从另一个名为 Node01.pc 的文件中获取数据并将这些值分配到我脚本中的变量中。 例如。

Node01.pc 包含:

192.168.0.99
2
70
16
80
4
4
100
4

怎么样

targets=($(cat Node01.pc))
echo "${targets[0]}"
echo "${targets[1]}"

编辑:如 Shawn 所述,这不适用于带有空格和斑点的行、空行 - 检查 https://mywiki.wooledge.org/DontReadLinesWithFor