Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
数组将“空格”作为默认分隔符:
str="HI I GOT;IT" arr2=$(echo $str | tr ";" " ") for x in $arr2 do echo " $x" done
输出:
你好
一世
拿到
它
我希望输出为:
嗨,我知道了
你还没有说这是哪个外壳,但它看起来像bash,所以我会去那个。这是 的工作IFS,它决定了如何bash拆分单词。在这里,我们将其设置;为单个命令,以拆分您的字符串。
bash
IFS
;
您还需要正确地遍历数组(使用引号和[@]),以便此时不会再次被 bash 拆分。
[@]
str="HI I GOT;IT" IFS=\; arr=($str) for x in "${arr[@]}" do echo "$x" done