我怎么用echo来做呢?

perl -E 'say "=" x 100'

当前回答

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

其他回答

大多数现有的解决方案都依赖于{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

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

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 3.0或更高版本中

for i in {1..100};do echo -n =;done
n=5; chr='x'; chr_string='';
for (( i=0; $i<$n; i++ ))
do
    chr_string=$chr_string$chr
done
echo -n "$chr_string"

适用于…… N =整数(包括0和负数)。 Chr =可打印和空白(空格和制表符)。

如果你想在echo和printf的不同实现之间遵循posix并保持一致性,和/或shell而不仅仅是bash:

seq(){ n=$1; while [ $n -le $2 ]; do echo $n; n=$((n+1)); done ;} # If you don't have it.

echo $(for each in $(seq 1 100); do printf "="; done)

...将在所有地方产生与perl -E 'say "=" x 100'相同的输出。