击:将字符串分割成字符数组 [英] Bash: Split string into character array

查看:129
本文介绍了击:将字符串分割成字符数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我想要分割成字符数组Bash的shell脚本一个字​​符串,而不是基于一个分隔符,但每个数组索引只是一个字符。我怎样才能做到这一点? <击>理想的情况下,将不使用任何外部程序。让我换一种说法。我的目标是便携性,所以像 SED 的东西,很可能是任何POSIX兼容的系统上都很好。

I have a string in a Bash shell script that I want to split into an array of characters, not based on a delimiter but just one character per array index. How can I do this? Ideally it would not use any external programs. Let me rephrase that. My goal is portability, so things like sed that are likely to be on any POSIX compatible system are fine.

推荐答案

尝试

echo "abcdefg" | fold -w1

编辑:增加了一个更优雅的解决方案,意见提出

echo "abcdefg" | grep -o .

这篇关于击:将字符串分割成字符数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆