how to have repeated command in bash code/script to represent the typing Alt n as the number of repetition followed by a key and then hit Enter?

e.g. have line code/script for (typing Alt 7) f (Enter)

  • 2
    I don't understand the question. Are you trying to write a script that sends a key or sequence of keys to another program using xdotool or similar? Or an interactive script where the user could type that, and then … what? Or do you want your terminal emulator/shell to expand that key sequence to a sequence of keys as input when used interactively?
    – frabjous
    Commented Sep 3, 2022 at 16:04

1 Answer 1


If you mean, repeat one character n times, you can do:

printf -v string X%.0s {1..12}

Would store a sequence of 12 Xs in $string.


$ echo "$(printf X%.0s {1..12})"

Though that implies forking an extra process.

You could also use a helper function like:

repeat_string() {
  awk -- 'BEGIN{for (i = 0; i < ARGV[1]; i++) printf "%s", ARGV[2]}' "$@"

And then:

echo "$(repeat_string 12 X)"

In zsh, you can use its padding operators:

$ echo ${(l[12][X])}

That's a null expansion left padded to a length of 12 with Xs. There's a similar right padding parameter expansion flag.

For count and character stored in variables:

n=12 c=X
echo ${(pl[$n][$c])}

zsh also has a repeat keyword (inspired from tcsh's):

$ echo "$(repeat 12 printf %s X)"

With bash, you could always write a repeat function that can repeat simple commands:

repeat() {
  local n="$1"
  while ((n-- > 0)); do

And then echo "$(repeat 12 printf %s X)" like above.

You could emulate a repeat keyword to repeat compound commands with an alias:

alias repeat='i=0; while test "$((i++))" -lt'

And then do echo "$(repeat 12; do printf %s X; done)" for instance.


You must log in to answer this question.