我怎么用echo来做呢?

perl -E 'say "=" x 100'

当前回答

我的答案有点复杂,可能并不完美,但对于那些希望输出大数字的人来说,我能够在3秒内完成大约1000万。

repeatString(){
    # argument 1: The string to print
    # argument 2: The number of times to print
    stringToPrint=$1
    length=$2

    # Find the largest integer value of x in 2^x=(number of times to repeat) using logarithms
    power=`echo "l(${length})/l(2)" | bc -l`
    power=`echo "scale=0; ${power}/1" | bc`

    # Get the difference between the length and 2^x
    diff=`echo "${length} - 2^${power}" | bc`

    # Double the string length to the power of x
    for i in `seq "${power}"`; do 
        stringToPrint="${stringToPrint}${stringToPrint}"
    done

    #Since we know that the string is now at least bigger than half the total, grab however many more we need and add it to the string.
    stringToPrint="${stringToPrint}${stringToPrint:0:${diff}}"
    echo ${stringToPrint}
}

其他回答

这是以利亚·卡根所支持的观点的长版本:

while [ $(( i-- )) -gt 0 ]; do echo -n "  "; done

当然,你也可以使用printf,但不是我喜欢的:

printf "%$(( i*2 ))s"

这个版本与Dash兼容:

until [ $(( i=i-1 )) -lt 0 ]; do echo -n "  "; done

I是初始数。

不是堆砌,而是另一种纯bash方法利用了数组的${//}替换:

$ arr=({1..100})
$ printf '%s' "${arr[@]/*/=}"
====================================================================================================

下面是我在linux中用来在屏幕上打印一行字符的方法(基于终端/屏幕宽度)

在屏幕上输入“=”:

printf '=%.0s' $(seq 1 $(tput cols))

解释:

打印等号的次数与给定序列相同:

printf '=%.0s' #sequence

使用命令的输出(这是bash的一个叫做命令替换的特性):

$(example_command)

给出一个序列,我以1到20为例。在最后一个命令中,使用tput命令代替20:

seq 1 20

给出终端中当前使用的列数:

tput cols

我的答案有点复杂,可能并不完美,但对于那些希望输出大数字的人来说,我能够在3秒内完成大约1000万。

repeatString(){
    # argument 1: The string to print
    # argument 2: The number of times to print
    stringToPrint=$1
    length=$2

    # Find the largest integer value of x in 2^x=(number of times to repeat) using logarithms
    power=`echo "l(${length})/l(2)" | bc -l`
    power=`echo "scale=0; ${power}/1" | bc`

    # Get the difference between the length and 2^x
    diff=`echo "${length} - 2^${power}" | bc`

    # Double the string length to the power of x
    for i in `seq "${power}"`; do 
        stringToPrint="${stringToPrint}${stringToPrint}"
    done

    #Since we know that the string is now at least bigger than half the total, grab however many more we need and add it to the string.
    stringToPrint="${stringToPrint}${stringToPrint:0:${diff}}"
    echo ${stringToPrint}
}

我怎么用echo来做呢?

如果echo后面跟着sed,你可以用echo来实现:

echo | sed -r ':a s/^(.*)$/=\1/; /^={100}$/q; ba'

实际上,这个回声在这里是不必要的。