![for loop in batch script example for loop in batch script example](https://media.geeksforgeeks.org/wp-content/uploads/20211102143411/batc1.png)
![for loop in batch script example for loop in batch script example](https://windowstect.com/wp-content/uploads/2021/06/image-141.png)
You can break out of a for loop using ‘break’ command as shown below. The same argument that is used in the ls command above, can be used in a bash for loop, as shown in the example below. The following will display all the *.conf file that begins with either a, b, or, c or d under /etc directory. This is the concept that is used in the above for5.sh example. Usage of * in the bash for loop is similar to the file globbing that we use in the linux command line when we use ls command (and other commands).įor example, the following will display all the files and directories under your home directory. The following example will loop through all the files and directories under your home directory. To loop through files and directories under a specific directory, just cd to that directory, and give * in the for loop as shown below. Loop through files and directories in a for loop $ cat for4.shįor username in `awk -F: '' /etc/passwd` You can use the output of any UNIX / Linux command as list of values to the for loop by enclosing the command in back-ticks ` ` as shown below. Unix command output as list values after “in” keyword Note: Refer to our earlier article to understand more about bash positional parameters. i.e for loop will never get executed as shown in the example below. If you leave the keyword “in” without any values, it will not use the positional parameter as shown below. You should not include the keyword “in” in the for loop. $ cat for3.shĬaution: Please be careful if you use this method. If you don’t specify the keyword “in” followed by any list of values in the bash for loop, it will use the positional parameters (i.e the arguments that are passed to the shell script). Don’t specify the list get it from the positional parameters Be careful and do not double quote your variable in the for loop. If you double quote the variable in this for loop, the list of values will be treated as single value. There are few exceptions to this best practice rule. $ cat for2.shĬaution: As a best practice, you should always quote the bash variables when you are referring it. Instead of providing the values directly in the for loop, you can store the values in a variable, and use the variable in the for loop after the “in” keyword, as shown in the following example. If you enclose in double quote, it will be treated as a single value (instead of 5 different values), as shown in the example below. $ cat for1-wrong1.shĬaution: The list of values should not be enclosed in a double quote. i.e Instead of “Mon”, it will use “Mon,” as value as shown in the example below. The comma will be treated as part of the value. $ cat for1.shĬaution: The list of values should not be separated by comma (Mon, Tue, Wed, Thu, Fri). In the following example, the list of values (Mon, Tue, Wed, Thu and Fri) are directly given after the keyword “in” in the bash for loop. Static values for the list after “in” keyword
#For loop in batch script example how to
The following 12 examples shows how to bash for loops in different ways. This is usually used to increment a loop counter.
![for loop in batch script example for loop in batch script example](https://docs.infoblox.com/download/attachments/3249271/complete_job_execution.png)
After each iteration of the loop, expr3 is evaluated.All the statements between do and done are executed repeatedly until the value of expr2 is TRUE.This is usually used to initialize variables for the loop.
![for loop in batch script example for loop in batch script example](https://wishmesh.com/wp-content/uploads/2015/02/infinite-loop-bat-and-dir-command-300x152.jpg)