Simple while loop with count in shell script

From Notes_Wiki

Home > Shell scripting > Useful bash shell scripts > Simple while loop with count in shell script

If we need to execute something specific no. of times, we can use while loop with count such as:

q=0; while [[ q -lt 3 ]]; do ((q++)); echo $q; done

Here update value of limit from 3 to the desired number of loop executions. Also instead of echo $q; put the command to be executed again and again. For example to generate 10 random passwords using mkpasswd use:

q=0; while [[ q -lt 10 ]]; do ((q++)); mkpasswd -l 10 -s 2; done


Home > Shell scripting > Useful bash shell scripts > Simple while loop with count in shell script