我怎么用echo来做呢?
perl -E 'say "=" x 100'
我怎么用echo来做呢?
perl -E 'say "=" x 100'
当前回答
for i in {1..100}
do
echo -n '='
done
echo
其他回答
下面是我在linux中用来在屏幕上打印一行字符的方法(基于终端/屏幕宽度)
在屏幕上输入“=”:
printf '=%.0s' $(seq 1 $(tput cols))
解释:
打印等号的次数与给定序列相同:
printf '=%.0s' #sequence
使用命令的输出(这是bash的一个叫做命令替换的特性):
$(example_command)
给出一个序列,我以1到20为例。在最后一个命令中,使用tput命令代替20:
seq 1 20
给出终端中当前使用的列数:
tput cols
没有简单的办法。使用printf和substitution避免循环。
str=$(printf "%40s")
echo ${str// /rep}
# echoes "rep" 40 times.
一种纯粹的Bash方式,没有eval,没有subshell,没有外部工具,没有大括号展开(即,你可以在变量中重复数字):
如果给你一个变量n,它展开为一个(非负的)数字和一个变量模式,例如,
$ n=5
$ pattern=hello
$ printf -v output '%*s' "$n"
$ output=${output// /$pattern}
$ echo "$output"
hellohellohellohellohello
你可以用它来创建一个函数:
repeat() {
# $1=number of patterns to repeat
# $2=pattern
# $3=output variable name
local tmp
printf -v tmp '%*s' "$1"
printf -v "$3" '%s' "${tmp// /$2}"
}
这套:
$ repeat 5 hello output
$ echo "$output"
hellohellohellohellohello
对于这个小技巧,我们经常使用printf:
-v varname: instead of printing to standard output, printf will put the content of the formatted string in variable varname. '%*s': printf will use the argument to print the corresponding number of spaces. E.g., printf '%*s' 42 will print 42 spaces. Finally, when we have the wanted number of spaces in our variable, we use a parameter expansion to replace all the spaces by our pattern: ${var// /$pattern} will expand to the expansion of var with all the spaces replaced by the expansion of $pattern.
你也可以通过间接展开来去掉repeat函数中的tmp变量:
repeat() {
# $1=number of patterns to repeat
# $2=pattern
# $3=output variable name
printf -v "$3" '%*s' "$1"
printf -v "$3" '%s' "${!3// /$2}"
}
建议的Python解决方案的更优雅的替代方案可能是:
python -c 'print "="*(1000)'
有不止一种方法。
使用循环:
大括号展开可用于整型字面值: 对于I在{1..100};执行echo -n =;完成 类c循环允许使用变量: 开始= 1 结束= 100 ((我= $开始;我< = $结束;我+ +));执行echo -n =;完成
使用内置的printf:
printf '=%.0s' {1..100}
在这里指定精度将截断字符串以适应指定的宽度(0)。当printf重用格式字符串以使用所有参数时,这将简单地打印"=" 100次。
使用head (printf, etc)和tr:
head -c 100 < /dev/zero | tr '\0' '='
printf %100s | tr " " "="