Bash 这里的多行字符串只产生一行

Bash 这里的多行字符串只产生一行,bash,Bash,我需要在whiledo循环中处理一个sting数组,计算一个值并在循环外使用它。首先,我编写了以下代码: git diff-index --cached HEAD | while read -r LINE; do ... done 但是,当然,它不保留内部变量值。然后,根据我在这里找到的建议,我使用了输入重定向: while read -r LINE; do ... done <<<$(git diff-index --cached HEAD) 读取-r行时;做 ..

我需要在whiledo循环中处理一个sting数组,计算一个值并在循环外使用它。首先,我编写了以下代码:

git diff-index --cached HEAD | while read -r LINE; do
   ...
done
但是,当然,它不保留内部变量值。然后,根据我在这里找到的建议,我使用了输入重定向:

while read -r LINE; do
...
done <<<$(git diff-index --cached HEAD)
读取-r行时
;做
...

完成您使用的是
您走的是正确的道路,您只需要在
git
生成的输出周围加上引号,以便将其正确地视为单个多行字符串:

while read -r LINE; do
...
done <<< "$(git diff-index --cached HEAD)"
读取-r行时
;做
...

做了相当详细和清楚的回答,它确实帮助了我。谢谢!
while read -r LINE; do
...
done <<< "$(git diff-index --cached HEAD)"
# "one" is passed on stdin, nothing happens
# "two" and "three" are passed as arguments, and echoed to stdout
$ echo <<< one two three
two three

# "one" is passed on stdin, gets printed to stdout
# "two" and "three" are passed as arguments, cat thinks they are filenames
$ cat <<< one two three 
cat: two: No such file or directory
cat: three: No such file or directory

# "one two three" is passed on stdin
# echo is invoked with no arguments and prints a blank line
$ echo <<< "one two three"

# "one two three" is passed on stdin
# cat is invoked with no arguments and prints whatever comes from stdin
$ cat <<< "one two three"
one two three