我怎么用echo来做呢?
perl -E 'say "=" x 100'
我怎么用echo来做呢?
perl -E 'say "=" x 100'
当前回答
我猜这个问题的最初目的是仅用shell的内置命令来完成这个任务。因此,for循环和printfs是合法的,而下面的rep、perl和jot则不是。但是,下面的命令
$((COLUMNS/2))
例如,打印的窗户完全线 \/\/\/\/\/\/\/\/\/\/\/\/
其他回答
你可以使用:
printf '=%.0s' {1..100}
这是如何工作的:
Bash扩展{1..100},那么命令就变成:
printf '=%.0s' 1 2 3 4 ... 100
我已经将printf的格式设置为=%。这意味着无论给出什么参数,它总是打印一个=。因此它输出100 =s。
最简单的方法是在bash中使用这一行代码:
seq 10 | xargs -n 1 | xargs -I {} echo -n ===\>;echo
在bash 3.0或更高版本中
for i in {1..100};do echo -n =;done
我的答案有点复杂,可能并不完美,但对于那些希望输出大数字的人来说,我能够在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 -e ''$_{1..100}'\b='
这将与perl -E 'say "=" x 100'完全相同,但只使用echo。