公告
财富商城
积分规则
提问
发文
2019-01-02 14:25发布
梦醉为红颜
How could I do this with echo?
echo
perl -E 'say "=" x 100'
This is the longer version of what Eliah Kagan was espousing:
while [ $(( i-- )) -gt 0 ]; do echo -n " "; done
Of course you can use printf for that as well, but not really to my liking:
printf "%$(( i*2 ))s"
This version is Dash compatible:
until [ $(( i=i-1 )) -lt 0 ]; do echo -n " "; done
with i being the initial number.
Python is ubiquitous and works the same everywhere.
python -c "import sys; print('*' * int(sys.argv[1]))" "=" 100
Character and count are passed as separate parameters.
I've just found a seriously easy way to do this using seq:
UPDATE: This works on the BSD seq that comes with OS X. YMMV with other versions
seq
seq -f "#" -s '' 10
Will print '#' 10 times, like this:
##########
-f "#"
#
-s ''
-f
-s
EDIT: Here it is in a handy function...
repeat () { seq -f $1 -s '' $2; echo }
Which you can call like this...
repeat "#" 10
NOTE: If you're repeating # then the quotes are important!
If you want POSIX-compliance and consistency across different implementations of echo and printf, and/or shells other than just bash:
printf
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)
...will produce the same output as perl -E 'say "=" x 100' just about everywhere.
In bash 3.0 or higher
for i in {1..100};do echo -n =;done
No easy way. But for example:
seq -s= 100|tr -d '[:digit:]'
Or maybe a standard-conforming way:
printf %100s |tr " " "="
There's also a tput rep, but as for my terminals at hand (xterm and linux) they don't seem to support it:)
tput rep
最多设置5个标签!
This is the longer version of what Eliah Kagan was espousing:
Of course you can use printf for that as well, but not really to my liking:
This version is Dash compatible:
with i being the initial number.
Python is ubiquitous and works the same everywhere.
python -c "import sys; print('*' * int(sys.argv[1]))" "=" 100
Character and count are passed as separate parameters.
I've just found a seriously easy way to do this using seq:
UPDATE: This works on the BSD
seq
that comes with OS X. YMMV with other versionsWill print '#' 10 times, like this:
-f "#"
sets the format string to ignore the numbers and just print#
for each one.-s ''
sets the separator to an empty string to remove the newlines that seq inserts between each number-f
and-s
seem to be important.EDIT: Here it is in a handy function...
Which you can call like this...
NOTE: If you're repeating
#
then the quotes are important!If you want POSIX-compliance and consistency across different implementations of
echo
andprintf
, and/or shells other than justbash
:...will produce the same output as
perl -E 'say "=" x 100'
just about everywhere.In bash 3.0 or higher
No easy way. But for example:
Or maybe a standard-conforming way:
There's also a
tput rep
, but as for my terminals at hand (xterm and linux) they don't seem to support it:)