How can I repeat the character -
n times in shell? I have read and tried this, but this does not work for -
. It throws error invalid option
. Below is the exact command that I used:
printf '-%.0s' {1..100}
Original posted line: printf '-%0.s' {1..100}
I also tried escaping -
by putting a \
but in that case it repeats \-
n times.
You can also use
tput
along withprintf
to accomplish filling the terminal with an exact number of dashes, either drawing the line directly, or writing the line of dashes to a variable (sayline
) for repeated use, e.g. to write a screen width line of dashes to the variableline
, you could do:and then simply
echo "$line"
each time you need to draw the line. You can also write it directly to the screen with.(I'm not a huge fan of
eval
, but this is one use where you are guaranteed the result of the command cannot be harmful).jot
can do it, with no bashisms:seq
too, but not as well, it needs atr
:John1024's helpful answer provides a generic solution that shows how to disambiguate options from operands for all POSIX-like utilities.
In the case at hand, the simplest solution is (works not only in
bash
, but also inksh
andzsh
):Placing
%.0s
before the-
avoids the issue of an initial-
getting mistaken for an option.Slightly optimized:[1]
[1]
%.0s
is in practice the most portable form (to be fully portable, you must also avoid the brace expansion,{...}
).%.s
, the equivalent shorthand form, is supported bybash
,ksh
, anddash
, but notzsh
<= v5.2 - even though it is equally POSIX-compliant : "The precision [the part after.
] shall take the form of a ( '.' ) followed by a decimal digit string; a null digit string is treated as zero."As a side note: The question originally contained a benign (in Bash) typo that sparked a debate:
%0.s
instead of%.0s
:%0.s
should effectively be the same as%.0s
, and for that matter, the same as%.s
and%0.0s
(all effectively request: print a [minimum zero-width] field filled with a zero-length string), but in practice isn't:zsh
<= v5.2 doesn't handle%0.s
correctly (again, due to the.s
part).Similarly, the GNU
printf
external-utility implementation (/usr/bin/printf
), as of GNU coreutils v8.24, reports an error with%0.s
, because it generally doesn't accept a field width of0
withs
:invalid conversion specification
- this matters for lesser-known shells that don't provideprintf
as a builtin. Note that the BSD/OSX implementation does not have this problem.Both
zsh
's (<= v5.2) behavior with%.s
and GNU/usr/bin/printf
's behavior with%0s
are deviations from the POSIX spec that smell like bugs.This question asks about
zsh
's behavior regarding%.s
, and the bug has since been confirmed and reported as fixed via a post-v5.2 commit that has yet to be released as of this writing.Use a
for
loop and number range:Or on a single line:
which outputs
----------
.EDIT: This was before your
printf
edit.This throws an error:
This works fine under
bash
:For other shells, try:
The problem was the
printf
expects that-
starts an option. As is common among Unix/POSIX utilities in this type of situation,--
signals toprintf
to expect no more options.I would recommend using a traditional
for
loop, as there is no need to spawn sub-processes or expand 100 arguments:It is curious that
printf -%s
triggers "invalid option" butprintf -
does not. To perhaps be extra safe, you could doprintf %s -
.