gpt4 book ai didi

bash - 在 Bash 中将定界字符串解析为数组 - 为什么 "$var"与 $var 不同,尽管 $var 没有空格?

转载 作者:行者123 更新时间:2023-11-29 09:12:49 25 4
gpt4 key购买 nike

<分区>

我正在运行 Bash 4.2.25 版。这是我的代码:

#!/usr/bin/env bash

string="one:two:three:four"

# without quotes
IFS=: read -ra array_1 <<< $string
for i in "${array_1[@]}"; do printf "i = [$i]\n"; done
# output:
# i = [one two three four]

# with quotes
IFS=: read -ra array_2 <<< "$string"
for i in "${array_2[@]}"; do printf "i = [$i]\n"; done
# output:
# i = [one]
# i = [two]
# i = [three]
# i = [four]

如何解释行为上的差异?

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