如何在不使用外部命令的情况下通过Unix Shell中的字符串字符迭代?
这不是bash。这是sh。 $ {foo:0:1}不起作用。导致不良替代。
foo =“ Hello”
如何单独打印每个字符?我正在寻找一种不需要外部命令的方法。
This is not bash. This is sh. ${foo:0:1} doesn't work. Results in bad substitution.
foo="Hello"
How do i print each character individually? I am looking for an approach that requires no external commands.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
扩展我的评论:传统的伯恩·贝壳(Bourne Shell)确实没有更现代的外壳可用的弦乐操作的内置设施。您将依靠外部程序来获得这些功能,这是 。
例如,使用
剪切
之类的简单内容,我们可以写:哪个将输出:
cut
之类的命令是“标准” shell scripting命令,尽管没有内置在shell中本身。Expanding on my comment: the traditional Bourne shell doesn't really have the sort of built-in facilities for string manipulation that are available in more modern shells. It was expected that you would rely on external program for these features.
For example, using something as simple as
cut
, we could write:Which would output:
Commands like
cut
are "standard" shell scripting commands, despite not being built into the shell itself.如 larsks 答案您必须依靠外部命令。
我建议使用单个
awk
调用,而不是一次运行外部程序以获取字符串长度,而是对每个索引反复重复。当然
,您可以将进一步的处理添加到
awk
脚本或读取和处理输出之类的输出,例如请参见 https://mywiki.wooledge.org/bashfaq/001
As written in larsks's answer you have to rely on external commands.
Instead of running external programs once to get the string length and repeatedly for every index, I suggest to use a single
awk
call.which prints
Of course you could add further processing to the
awk
script or read and process the output likesee https://mywiki.wooledge.org/BashFAQ/001
建议
awk
:建议
bash
:suggesting
awk
:suggesting
bash
: