gpt4 book ai didi

bash - 如何在 bash 中将一个空分隔字符串传递到一个数组中?

转载 作者:行者123 更新时间:2023-11-29 09:01:04 24 4
gpt4 key购买 nike

给定一个输出由字节分隔的多个字符串的命令,在 bash 中将其转换为数组的最佳(最快)方法是什么。

例如:git ls-files -z

最佳答案

仅适用于 bash 4.4 及更高版本:

readarray -d '' array < <(git ls-files -z)

为了向后兼容 bash 3.x 和 4.0 到 4.3:

array=( )
while IFS= read -r -d '' item; do
array+=( "$item" )
done < <(git ls-files -z)

关于bash - 如何在 bash 中将一个空分隔字符串传递到一个数组中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54127688/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com