重击:重复字符多次

编程入门 行业动态 更新时间:2024-10-24 18:22:45
本文介绍了重击:重复字符多次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

根据以下问题和反省:

unix.stackexchange/questions/188658/使用-printf命令编写一个字符n次-

如何在bash中重复一个字符?

我想学习如何设置一个字符/字符串的重复值.例如,以下代码非常有效:

I would like to learn how one might go about parameterizing the repeat value for a character/string. For example, the followings works spiffingly:

printf " ,\n%0.s" {1..5}

但是,如果我想参数化"5",请说:

However, if I wanted to parameterize '5', say:

num=5

我似乎无法正确进行扩展才能完成此工作.例如:

I cannot seem to get the expansion correct to make this work. For instance:

printf " ,\n%0.s" {1..$((num))}

失败.

任何想法/想法都将受到欢迎-我认为有一种方法可以做到这一点而不必求助于perl或awk,所以只是对poss感到好奇.

Any thoughts/ideas would be most welcome - I reckon there's a way to do this without having to resort to perl or awk so just curious if poss.

谢谢!

推荐答案

如果可以将命令构建为字符串(具有所需的所有参数扩展),则可以对其进行评估.这将打印X num 次:

If you can build the command as a string -- with all the parameter expansion you want -- then you can evaluate it. This prints X num times:

num=10 eval $(echo printf '"X%0.s"' {1..$num})

更多推荐

重击:重复字符多次

本文发布于:2023-11-22 20:57:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1618995.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!