bash 在bash中重复打印一个字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5799303/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 20:28:20  来源:igfitidea点击:

Print a character repeatedly in bash

bashshell

提问by Ankur Agarwal

Is there a way to print same character repeatedly in bash, just like you can use this construct to do this in python:

有没有办法在 bash 中重复打印相同的字符,就像你可以使用这个构造在 python 中执行此操作一样:

print('%' * 3)

gives

%%%

采纳答案by ghostdog74

sure, just use printfand a bit of bash string manipulation

当然,只需使用printf和一些 bash 字符串操作

$ 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=
 num=
 v=$(printf "%-${num}s" "$str")
 echo "${v// /*}"
}

Test run:

测试运行:

$ printf_new "*" 20
********************
$ printf_new "*" 10
**********
$ printf_new "%" 10
%%%%%%%%%%

回答by CaffeineConnoisseur

There's actually a one-liner that can do this:

实际上有一个单线可以做到这一点:

    printf "%0.s-" {1..10}

prints

印刷

    ----------

Here's the breakdown of the arguments passed to printf:

以下是传递给 的参数的细分printf

  • %s - This specifies a string of any length
  • %0s - This specifies a string of zero length, but if the argument is longer it will print the whole thing
  • %0.s - This is the same as above, but the period tells printfto truncate the string if it's longer than the specified length, which is zero
  • {1..10} - This is a brace expansion that actually passes the arguments "1 2 3 4 5 6 7 8 9 10"
  • "-" - This is an extra character provided to printf, it could be anything (for a "%" you must escape it with another "%" first, i.e. "%%")
  • Lastly, The default behavior for printfif you give it more arguments than there are specified in the format string is to loop back to the beginning of the format string and run it again.
  • %s - 指定任意长度的字符串
  • %0s - 这指定了一个零长度的字符串,但如果参数更长,它将打印整个内容
  • %0.s - 这与上面相同,但句点告诉printf如果字符串长于指定长度(为零)则截断字符串
  • {1..10} - 这是一个大括号扩展,它实际上传递了参数“1 2 3 4 5 6 7 8 9 10”
  • "-" - 这是提供给 的一个额外字符printf,它可以是任何东西(对于“%”,您必须先用另一个“%”对其进行转义,即“%%”)
  • 最后,printf如果给它的参数多于格式字符串中指定的参数,默认行为是循环回到格式字符串的开头并再次运行它。

The end result of what's going on here then is that you're telling printfthat you want it to print a zero-length string with no extra characters if the string provided is longer than zero. Then after this zero-length string print a "-" (or any other set of characters). Then you provide it 10 arguments, so it prints 10 zero-length strings following each with a "-".

这里发生的事情的最终结果是,printf如果提供的字符串长于零,则您要告诉它打印一个零长度的字符串,没有额外的字符。然后在这个零长度字符串之后打印一个“-”(或任何其他字符集)。然后你给它提供 10 个参数,所以它打印 10 个零长度字符串,每个字符串后面都有一个“-”。

It's a one-liner that prints any number of repeating characters!

这是一个单行打印任意数量的重复字符!

Edit:

编辑:

Coincidentally, if you want to print $variablecharacters you just have to change the argument slightly to use seqrather than brace expansion as follows:

巧合的是,如果您想打印$variable字符,您只需稍微更改参数即可使用seq而不是大括号扩展,如下所示:

    printf '%0.s-' $(seq 1 $variable)

This will instead pass arguments "1 2 3 4 ... $variable" to printf, printing precisely $variableinstances of "-"

这将改为将参数“1 2 3 4 ... $variable”传递给printf,精确打印$variable“-”的实例

回答by Deadcode

The current accepted answer for this question (by ghostdog74) provides a method that executes extremely slowly for even a moderately high number of characters. The top-voted answer (by CaffeineConnoisseur) is better, but is still quite slow.

这个问题的当前接受的答案(由ghostdog74提供)提供了一种方法,即使对于中等数量的字符,它的执行速度也非常缓慢。最高投票的答案(由CaffeineConnoisseur 提供)更好,但仍然很慢。

Here is what, in my tests, has executed fastest of all (even faster than the python version):

这是在我的测试中执行得最快的(甚至比 python 版本更快):

perl -E "print '*' x 1000"

In second place was the python command:

排在第二位的是 python 命令:

python -c "print('*' * 1000)"

If neither perl nor python are available, then this is third-best:

如果 perl 和 python 都不可用,那么这是第三好的:

head -c 1000 /dev/zero | tr '
printf '%*s' 1000 | tr ' ' '*'
' '*'

And in fourth place is the one using the bash printfbuilt-in along with tr:

排在第四位的是使用printf内置的 bash以及tr

printf '%.s*' {1..1000}

And here's one (from CaffeineConnoisseur's answer) that's in fifth place in speed for large numbers of repeated characters, but might be faster for small numbers (due to using only a bash built-in, and not spawning an external process):

这是一个(来自CaffeineConnoisseur的回答)对于大量重复字符的速度排在第五位,但对于小数字可能更快(因为只使用内置的 bash,而不是产生外部进程):

echo $(yes % | head -n3)

回答by manojlds

I like this:

我喜欢这个:

for ((i=0; i<3; i++)){
   echo -ne "%"
}

You may not like this:

你可能不喜欢这样:

s=$( printf "%3s" ); echo " ${s// /%}"

You might like this:

你可能喜欢这个:

echo %{,,}

Source: http://dbaspot.com/shell/357767-bash-fast-way-repeat-string.html

来源:http: //dbaspot.com/shell/357767-bash-fast-way-repeat-string.html

There is also this form, but not very useful:

还有这种形式,但不是很有用:

$ for a in `seq 5`; do echo -n %; done
%%%%%

回答by Greg Hewgill

It's ugly, but you can do it like this:

这很丑陋,但你可以这样做:

head -c 20 /dev/zero |tr '
CACHE="$(head -c 1000 /dev/zero |tr '
head -c 20 /dev/zero |tr '
CACHE="$(head -c 1000 /dev/zero |tr '
char='%'
count=5
result=$( printf "%${count}s" ' ' )
echo -e ${result// /$char}
' '+')" echo "${CACHE:0:10}" echo "${CACHE:0:100}" echo "${CACHE:0:300}"
' '+'
' '+')" echo "${CACHE:0:10}" echo "${CACHE:0:100}" echo "${CACHE:0:300}"
' '+'

Of course, seqis an external program (which you probably have).

当然,seq是一个外部程序(你可能有)。

回答by ssvda

  1. It is possible to obtain any number of zero (\0) character from /dev/zero. The only thing left to do it is

    #!/usr/bin/awk -f
    BEGIN {
      while (c++ < 3) printf "%"
    }
    

    Note that headand trare external commands. So that it would be invoked in separate process.

  2. It is possible to "optimize" this solution with string caching.

    %%%
    

    There are only bashbuilt-ins in the echo statement. So, we can use it in cycle.

  1. 可以\0/dev/zero. 剩下要做的就是

    seq -s % 4|tr -d '[:digit:]'
    

    请注意,headtr是外部命令。这样它就会在单独的进程中被调用。

  2. 可以使用字符串缓存来“优化”此解决方案。

    ##代码##

    bashecho 语句中只有内置函数。所以,我们可以循环使用它。

回答by Fritz G. Mehner

Another one:

另一个:

##代码##

回答by Steven Penny

##代码##

Result

结果

##代码##

回答by HovoK

Here is the most simple way to do that

这是最简单的方法

##代码##

Note there will be only 3 '%' in created sequence.

请注意,创建的序列中将只有 3 个“%”。