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)
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)
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.
Or:
$ echo "$(printf X%.0s {1..12})"
XXXXXXXXXXXX
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])}
XXXXXXXXXXXX
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)"
XXXXXXXXXXXX
With bash, you could always write a repeat function that can repeat simple commands:
repeat() {
local n="$1"
shift
while ((n-- > 0)); do
"$@"
done
}
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.