shell - 循环遍历以逗号分隔的 shell 变量

标签 shell loops unix for-loop cut

假设我有一个 Unix shell 变量,如下所示

variable=abc,def,ghij

我想使用 for 循环提取所有值(abcdefghij),并将每个值传递到过程中。

该脚本应允许从 $variable 中提取任意数量的逗号分隔值。

最佳答案

不要搞乱 IFS
不调用外部命令

variable=abc,def,ghij
for i in ${variable//,/ }
do
    # call your procedure/other scripts here below
    echo "$i"
done

使用 bash 字符串操作 http://www.tldp.org/LDP/abs/html/string-manipulation.html

关于shell - 循环遍历以逗号分隔的 shell 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27702452/

相关文章:

r - 遍历全局环境中的对象

php - 从 php 触发 bash 脚本时,服务未执行

bash - 完全重置/重新加载 bash(别名和函数)

linux - 脚本运行另一个脚本

linux - 用另一个文件中的值替换文件中的字符串

java - java中的连接循环

c++ - 从选择的语句中删除选择

c - 为什么我在这个管道上得到垃圾?

shell - 如何获取大文件的行数,至少5G

linux - sshpass 中的多个密码