我怎么用echo来做呢?
perl -E 'say "=" x 100'
我怎么用echo来做呢?
perl -E 'say "=" x 100'
当前回答
repeat() {
# $1=number of patterns to repeat
# $2=pattern
printf -v "TEMP" '%*s' "$1"
echo ${TEMP// /$2}
}
其他回答
没有简单的办法。使用printf和substitution避免循环。
str=$(printf "%40s")
echo ${str// /rep}
# echoes "rep" 40 times.
最简单的方法是在csh/tcsh中使用这一行代码:
printf "%50s\n" '' | tr '[:blank:]' '[=]'
这里有两种有趣的方法:
ubuntu@ubuntu:~$ yes = | head -10 | paste -s -d '' - ========== ubuntu@ubuntu:~$ yes = | head -10 | tr -d "\n" ==========ubuntu@ubuntu:~$
注意这两个方法略有不同——粘贴方法以新行结束。tr方法没有。
不是堆砌,而是另一种纯bash方法利用了数组的${//}替换:
$ arr=({1..100})
$ printf '%s' "${arr[@]/*/=}"
====================================================================================================
没有简单的方法。但是举个例子:
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),它们似乎不支持它:)