Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I write a 'for' loop in Bash?

I'm looking for the basic loop like:

for(int i = 0; i < MAX; i++) {   doSomething(i); } 

but for Bash.

like image 622
John Meagher Avatar asked Sep 08 '08 03:09

John Meagher


People also ask

Is there a for loop in bash?

A bash for loop is a bash programming language statement which allows code to be repeatedly executed. A for loop is classified as an iteration statement i.e. it is the repetition of a process within a bash script. For example, you can run UNIX command or task 5 times or read and process list of files using a for loop.

How do you write a loop in shell script?

1) Syntax:Syntax of for loop using in and list of values is shown below. This for loop contains a number of variables in the list and will execute for each item in the list. For example, if there are 10 variables in the list, then loop will execute ten times and value will be stored in varname.

What does [- Z $1 mean in bash?

$1 means an input argument and -z means non-defined or empty. You're testing whether an input argument to the script was defined when running the script. Follow this answer to receive notifications.


1 Answers

From this site:

for i in $(seq 1 10); do     echo $i done 
like image 53
Rob Rolnick Avatar answered Sep 25 '22 21:09

Rob Rolnick