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
Gerg Wooledge in
<https://lists.gnu.org/archive/html/bug-bash/2025-09/msg00235.html>.
---
examples/startup-files/Bash_aliases | 29 +++++++++--------------------
1 file changed, 9 insertions(+), 20 deletions(-)
diff --git a/examples/startup-files/Bash_aliases
b/examples/startup-files/Bash_aliases
index 2abb93ee..dbe4353b 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 "$@";
- done
-}
+repeat() {
+ if (($# < 2)); then
+ echo "usage: repeat N command [arg] [...]" >&2
+ return 1
+ fi
-# Subfunction needed by `repeat'.
-seq ()
-{
- local lower upper output;
- lower=$1 upper=$2;
+ local n="$1" i
+ shift
- if [ $lower -ge $upper ]; then return; fi
- while [ $lower -le $upper ];
- do
- echo -n "$lower "
- lower=$(($lower + 1))
+ for ((i=1; i<=n; i++)); do
+ "$@"
done
- echo "$lower"
}
--
2.51.0