我怎么用echo来做呢?

perl -E 'say "=" x 100'

当前回答

最简单的方法是在bash中使用这一行代码:

seq 10 | xargs -n 1 | xargs -I {} echo -n  ===\>;echo

其他回答

如果你想重复一个字符n次,n是一个变量的次数,这取决于,比如说,字符串的长度,你可以这样做:

#!/bin/bash
vari='AB'
n=$(expr 10 - length $vari)
echo 'vari equals.............................: '$vari
echo 'Up to 10 positions I must fill with.....: '$n' equal signs'
echo $vari$(perl -E 'say "=" x '$n)

它显示:

vari equals.............................: AB  
Up to 10 positions I must fill with.....: 8 equal signs  
AB========  

Python无处不在,在任何地方都能正常工作。

import sys;Print ('*' * int(sys.argv[1]))"" = " 100

Character和count作为单独的参数传递。

function repeatString()
{
    local -r string="${1}"
    local -r numberToRepeat="${2}"

    if [[ "${string}" != '' && "${numberToRepeat}" =~ ^[1-9][0-9]*$ ]]
    then
        local -r result="$(printf "%${numberToRepeat}s")"
        echo -e "${result// /${string}}"
    fi
}

样本运行

$ repeatString 'a1' 10 
a1a1a1a1a1a1a1a1a1a1

$ repeatString 'a1' 0 

$ repeatString '' 10 

参考库:https://github.com/gdbtek/linux-cookbooks/blob/master/libraries/util.bash

在bash 3.0或更高版本中

for i in {1..100};do echo -n =;done

一种纯粹的Bash方式,没有eval,没有subshell,没有外部工具,没有大括号展开(即,你可以在变量中重复数字):

如果给你一个变量n,它展开为一个(非负的)数字和一个变量模式,例如,

$ n=5
$ pattern=hello
$ printf -v output '%*s' "$n"
$ output=${output// /$pattern}
$ echo "$output"
hellohellohellohellohello

你可以用它来创建一个函数:

repeat() {
    # $1=number of patterns to repeat
    # $2=pattern
    # $3=output variable name
    local tmp
    printf -v tmp '%*s' "$1"
    printf -v "$3" '%s' "${tmp// /$2}"
}

这套:

$ repeat 5 hello output
$ echo "$output"
hellohellohellohellohello

对于这个小技巧,我们经常使用printf:

-v varname: instead of printing to standard output, printf will put the content of the formatted string in variable varname. '%*s': printf will use the argument to print the corresponding number of spaces. E.g., printf '%*s' 42 will print 42 spaces. Finally, when we have the wanted number of spaces in our variable, we use a parameter expansion to replace all the spaces by our pattern: ${var// /$pattern} will expand to the expansion of var with all the spaces replaced by the expansion of $pattern.


你也可以通过间接展开来去掉repeat函数中的tmp变量:

repeat() {
    # $1=number of patterns to repeat
    # $2=pattern
    # $3=output variable name
    printf -v "$3" '%*s' "$1"
    printf -v "$3" '%s' "${!3// /$2}"
}