shell使用split分割字符串生成数组
时间: 2024-03-21 10:39:31 浏览: 305
可以使用`split`命令将一个字符串分割成数组,具体命令如下:
```shell
str="apple,banana,orange"
arr=($(echo $str | tr ',' ' '))
echo ${arr[0]} # 输出 apple
echo ${arr[1]} # 输出 banana
echo ${arr[2]} # 输出 orange
```
其中,`echo $str | tr ',' ' '`将逗号替换为空格,然后使用`$()`将其作为一个命令执行,并将结果赋值给数组`arr`。`${arr[0]}`表示数组中的第一个元素,以此类推。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![application/x-zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)