Bash:将字符串拆分为字符数组 [英] Bash: Split string into character array

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

问题描述

我在 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 .

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

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