在bash多次打印字符 [英] Print a character repeatedly in bash
本文介绍了在bash多次打印字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法在bash重复打印相同的字符,就像你可以使用这个结构来做到这一点在python:
打印('%'* 3)
给
%%%
解决方案
肯定的是,只要使用的printf
和一点bash的字符串操作
$ S = $(的printf%-30s,*)
$回声$ {S // / *}
******************************
应该有一个较短的方式,但目前这是我怎么会做到这一点。您可以到这一点,你可以在日后使用
库存储功能 printf_new(){
STR = $ 1
NUM = $ 16
V = $(printf的% - $ {NUM}的$海峡)
回声$ {V // / *}
}
测试运行:
$ printf_new*20
********************
$ printf_new*10
**********
$ printf_new%10
%%%%%%%%%%
Is there a way to print same character repeatedly in bash, just like you can use this construct to do this in python:
print('%' * 3)
gives
%%%
解决方案
sure, just use printf
and a bit of bash string manipulation
$ s=$(printf "%-30s" "*")
$ echo "${s// /*}"
******************************
There should be a shorter way, but currently that's how i would do it. You can make this into a function which you can store in a library for future use
printf_new() {
str=$1
num=$2
v=$(printf "%-${num}s" "$str")
echo "${v// /*}"
}
Test run:
$ printf_new "*" 20
********************
$ printf_new "*" 10
**********
$ printf_new "%" 10
%%%%%%%%%%
这篇关于在bash多次打印字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文