相关疑难解决方法(0)

数组上的 shell 参数扩展

假设我将一些数据读入 Bash 数组:

$ IFS=" " read -a arr <<< "hello/how are/you iam/fine/yeah"
Run Code Online (Sandbox Code Playgroud)

现在,我想打印/数组中每个元素的第一个切片字段。

我所做的是循环遍历元素并使用 shell 参数扩展从第一个元素中删除所有内容/

$ for w in "${arr[@]}"; do echo "${w%%/*}"; done
hello
are
iam
Run Code Online (Sandbox Code Playgroud)

但是,sinceprintf允许我们在单个表达式中打印数组的全部内容:

$ printf "%s\n" "${arr[@]}"
hello/how
are/you
iam/fine
Run Code Online (Sandbox Code Playgroud)

...我想知道是否有一种方法可以${w%%/*}在使用时使用 shell 参数扩展printf,而不是循环遍历所有元素并对每个元素执行此操作。

arrays bash parameter-expansion

6
推荐指数
1
解决办法
5055
查看次数

标签 统计

arrays ×1

bash ×1

parameter-expansion ×1