Skip to content
TrySitePrice

Linux / UNIX: Run Command a Number of Times In a Row

Best/Tutorial

There are several ways to run a command a specified number of times in a row in Linux/Unix. Here are some of the most common methods:

  1. for loop:

You can use a for loop to run a command a specified number of times. For example, to run the ls command 5 times, you can use the following script:

for i in {1..5}; do

 ls

done

  1. while loop:

You can also use a while loop to run a command a specified number of times. For example, to run the ls command 5 times, you can use the following script:

count=0

while [ $count -lt 5 ]; do

 ls

 count=$((count + 1))

done

  1. seq command:

You can use the seq command to generate a list of numbers and use it to run a command a specified number of times. For example, to run the ls command 5 times, you can use the following script:

for i in $(seq 1 5); do

 ls

done

  1. yes command:

You can use the yes command to run a command a specified number of times. For example, to run the ls command 5 times, you can use the following script:

yes ls | head -n 5

These are some of the most common methods to run a command a specified number of times in Linux/Unix. Choose the method that works best for you and your specific needs.