Bash

Looping Through Commands in Bash

This article introduces how to execute commands in a loop and implement scripts in the Bash language on Linux PCs and servers. I wrote a script to execute commands multiple times with time intervals.

Shou Arisaka
1 min read
Nov 19, 2025

This article introduces how to execute commands in a loop and implement scripts in the Bash language on Linux PCs and servers.

I wrote a script to execute commands multiple times with time intervals.

loop(){

    : loop [times] [interval] [command[opts]]
  : e.g. loop 2 0 "learn-vocab ok ; ok ;"
  : e.g. loop 2 1 "command ls -1"
    : or
  : loop 2 1 command ls -1

  local times=$1
  local interval=$2
  shift
  shift

  for i in $( seq 1 ${times:-2} ) ;
  do
     eval "${@}"
     sleep "${interval}"
  done

}

Share this article

Shou Arisaka Nov 19, 2025

๐Ÿ”— Copy Links