Bash For LoopIn this topic, we will understand the usage of for loop in Bash scripts. Like any other programming language, bash shell scripting also supports 'for loops' to perform repetitive tasks. It helps us to iterate a particular set of statements over a series of words in a string, or elements in an array. For example, you can either run UNIX command (or task) many times or just read and process the list of commands using a 'for loop'. Syntax of For LoopWe can apply 'for loop' on bash script in two ways. One way is 'for-in' and another way is the c-style syntax. Following is the syntax of 'for loop' in bash shell scripting: Or There are some key points of 'for loop' statement:
Some of the 'for loop' examples are given below to illustrate how do they work: Basic 'For Loop' ExampleBash Script Output For Loop to Read a RangeBash Script Output For Loop to Read a Range with Increment/DecrementWe can increase or decrease a specified value by adding two another dots (..) and the value to step by, e.g., {START..END..INCREMENT}. Check out the example below: For Increment Output For Decreament Output For Loop to Read Array VariablesWe can use 'for loop' to iterate the values of an array. The syntax can be defined as: Output For each element in 'array', the statements or set of commands from 'do' till 'done' are executed. Each element could be accessed as 'i' within the loop for the respective iteration. Check out the example below explaining the use of 'for loop' to iterate over elements of an array: Bash Script Output For Loop to Read white spaces in String as word separatorsThe syntax can be defined as below: Here, str refers to a string. The statements from 'do' till 'done' are executed for each 'word' of a string. Check out the example below: Bash Script Output For Loop to Read each line in String as a wordThe syntax can be defined as below: Here, the statements from 'do' till 'done' are executed for each 'line' of a string. Check out the example below: Bash Script Output Note: The only difference between 'For Loop to Read white spaces in String as word separators' and 'For Loop to Read each line in String as a word' is the double quotes around string variable.For Loop to Read Three-expressionThree expression syntax is the most common syntax of 'for loop'. The first expression refers to the process of initialization, the second expression refers to the termination, and the third expression refers to the increment or decrement. Check out the example below to print 1 to 10 numbers using three expressions with for loop: Bash Script Output For Loop with a Break StatementA 'break' statement can be used inside 'for' loop to terminate from the loop. Bash Script Output For Loop with a Continue StatementWe can use the 'continue' statement inside the 'for' loop to skip any specific statement on a particular condition. It tells Bash to stop executing that particular iteration of the loop and process the next iteration. Bash Script Output Infinite Bash For LoopWhen there is no 'start, condition, and increment' in the bash three expressions for loop, it becomes an infinite loop. To terminate the infinite loop in Bash, we can press Ctrl+C. Bash Script Output ConclusionIn this topic, we discussed how to use for loop statement in Bash to perform specific tasks Next TopicBash While Loop |