我怎么用echo来做呢?
perl -E 'say "=" x 100'
我怎么用echo来做呢?
perl -E 'say "=" x 100'
当前回答
这里有两种有趣的方法:
ubuntu@ubuntu:~$ yes = | head -10 | paste -s -d '' - ========== ubuntu@ubuntu:~$ yes = | head -10 | tr -d "\n" ==========ubuntu@ubuntu:~$
注意这两个方法略有不同——粘贴方法以新行结束。tr方法没有。
其他回答
下面是我在linux中用来在屏幕上打印一行字符的方法(基于终端/屏幕宽度)
在屏幕上输入“=”:
printf '=%.0s' $(seq 1 $(tput cols))
解释:
打印等号的次数与给定序列相同:
printf '=%.0s' #sequence
使用命令的输出(这是bash的一个叫做命令替换的特性):
$(example_command)
给出一个序列,我以1到20为例。在最后一个命令中,使用tput命令代替20:
seq 1 20
给出终端中当前使用的列数:
tput cols
for i in {1..100}
do
echo -n '='
done
echo
大多数现有的解决方案都依赖于{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
如果你想在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'相同的输出。
我的建议(接受n的变量值):
n=100
seq 1 $n | xargs -I {} printf =