From: Greg Wooledge <[email protected]>
The `repeat` function calls the provided command one time too many:
repeat 10 echo "foo" | wc -l ⇒ 11
Also, the provided `seq` function shadows `/usr/bin/seq`.
This patch fixes this problem by adapting the changes suggested by
Greg Wooledge in
<https://lists.gnu.org/archive/html/bug-bash/2025-09/msg00235.html>.
---
examples/startup-files/Bash_aliases | 33 ++++++++++-------------------
1 file changed, 11 insertions(+), 22 deletions(-)
Revisions:
v2: Use version from Greg Wooledge
v3: eval "$@", more readable diff
This patch is available as the last commit on the
`upstream-pr/examples-aliases-repeat` branch of the Git repo
<https://salsa.debian.org/gioele/bash.git>.
diff --git a/examples/startup-files/Bash_aliases
b/examples/startup-files/Bash_aliases
index 2abb93ee..3c7294df 100644
--- a/examples/startup-files/Bash_aliases
+++ b/examples/startup-files/Bash_aliases
@@ -37,27 +37,16 @@ add-alias ()
# "repeat" command. Like:
#
# repeat 10 echo foo
-repeat ()
-{
- local count="$1" i;
- shift;
- for i in $(seq 1 "$count");
- do
- eval "$@";
+repeat() {
+ if (($# < 2)); then
+ echo "usage: repeat N command [arg] [...]" >&2
+ return 1
+ fi
+
+ local n="$1" i
+ shift
+
+ for ((i=1; i<=n; i++)); do
+ eval "$@"
done
}
-
-# Subfunction needed by `repeat'.
-seq ()
-{
- local lower upper output;
- lower=$1 upper=$2;
-
- if [ $lower -ge $upper ]; then return; fi
- while [ $lower -le $upper ];
- do
- echo -n "$lower "
- lower=$(($lower + 1))
- done
- echo "$lower"
-}
--
2.51.0