我怎么用echo来做呢?

perl -E 'say "=" x 100'

当前回答

大多数现有的解决方案都依赖于{1..shell的10}语法支持,这是bash和zsh特定的,并且不能在tcsh或OpenBSD的ksh和大多数非bash sh中工作。

以下代码适用于OS X和所有*BSD系统;实际上,它可以用来生成各种类型装饰空间的整体矩阵:

$ printf '=%.0s' `jot 64` | fold -16
================
================
================
================$ 

遗憾的是,我们没有得到一个尾随换行符;这可以通过在折叠后额外的printf '\n'来修复:

$ printf "=%.0s" `jot 64` | fold -16 ; printf "\n"
================
================
================
================
$ 

引用:

http://mdoc.su/-/printf.1 http://mdoc.su/-/jot.1 http://mdoc.su/-/fold.1

其他回答

我猜这个问题的最初目的是仅用shell的内置命令来完成这个任务。因此,for循环和printfs是合法的,而下面的rep、perl和jot则不是。但是,下面的命令

$((COLUMNS/2))

例如,打印的窗户完全线 \/\/\/\/\/\/\/\/\/\/\/\/

在bash 3.0或更高版本中

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

另一种表示任意字符串重复n次:

优点:

使用POSIX shell工作。 输出可以赋值给一个变量。 重复任何字符串。 即使有很大的重复也非常快。

缺点:

需要Gnu核心Utils的yes命令。

#!/usr/bin/sh
to_repeat='='
repeat_count=80
yes "$to_repeat" | tr -d '\n' | head -c "$repeat_count"

使用ANSI终端和重复的US-ASCII字符。您可以使用ANSI CSI转义序列。这是重复一个汉字最快的方法。

#!/usr/bin/env bash

char='='
repeat_count=80
printf '%c\e[%db' "$char" "$repeat_count"

或静态:

打印一行80次=:

printf’= e [80b \ n '

限制:

不是所有的终端都理解repeat_char ANSI CSI序列。 只能重复US-ASCII或单字节ISO字符。 在最后一列重复停止,因此可以使用较大的值来填充整行,而不管终端宽度如何。 重复只是为了显示。将输出捕获到shell变量中不会将repeat_char ANSI CSI序列扩展为重复字符。

没有简单的方法。但是举个例子:

seq -s= 100|tr -d '[:digit:]'
# Editor's note: This requires BSD seq, and breaks with GNU seq (see comments)

或者是一种符合标准的方式:

printf %100s |tr " " "="

还有一个tput代表,但对于我手头的终端(xterm和linux),它们似乎不支持它:)

我的答案有点复杂,可能并不完美,但对于那些希望输出大数字的人来说,我能够在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}
}