我怎么用echo来做呢?
perl -E 'say "=" x 100'
我怎么用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
其他回答
你可以使用:
printf '=%.0s' {1..100}
这是如何工作的:
Bash扩展{1..100},那么命令就变成:
printf '=%.0s' 1 2 3 4 ... 100
我已经将printf的格式设置为=%。这意味着无论给出什么参数,它总是打印一个=。因此它输出100 =s。
我的建议(接受n的变量值):
n=100
seq 1 $n | xargs -I {} printf =
不是堆砌,而是另一种纯bash方法利用了数组的${//}替换:
$ arr=({1..100})
$ printf '%s' "${arr[@]/*/=}"
====================================================================================================
Python无处不在,在任何地方都能正常工作。
import sys;Print ('*' * int(sys.argv[1]))"" = " 100
Character和count作为单独的参数传递。
建议的Python解决方案的更优雅的替代方案可能是:
python -c 'print "="*(1000)'