🕷️ Crawler Inspector

URL Lookup

Direct Parameter Lookup

Raw Queries and Responses

1. Shard Calculation

Query:
Response:
Calculated Shard: 103 (from laksa147)

2. Crawled Status Check

Query:
Response:

3. Robots.txt Check

Query:
Response:

4. Spam/Ban Check

Query:
Response:

5. Seen Status Check

ℹ️ Skipped - page is already crawled

📄
INDEXABLE
CRAWLED
3 days ago
🤖
ROBOTS ALLOWED

Page Info Filters

FilterStatusConditionDetails
HTTP statusPASSdownload_http_code = 200HTTP 200
Age cutoffPASSdownload_stamp > now() - 6 MONTH0.1 months ago
History dropPASSisNull(history_drop_reason)No drop reason
Spam/banPASSfh_dont_index != 1 AND ml_spam_score = 0ml_spam_score=0
CanonicalPASSmeta_canonical IS NULL OR = '' OR = src_unparsedNot set

Page Details

PropertyValue
URLhttps://www.geeksforgeeks.org/linux-unix/looping-statements-shell-script/
Last Crawled2026-04-10 02:12:05 (3 days ago)
First Indexed2025-06-12 18:16:29 (10 months ago)
HTTP Status Code200
Meta TitleLooping Statements | Shell Script - GeeksforGeeks
Meta DescriptionYour All-in-One Learning Portal: GeeksforGeeks is a comprehensive educational platform that empowers learners across domains-spanning computer science and programming, school education, upskilling, commerce, software tools, competitive exams, and more., Your All-in-One Learning Portal. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
Meta Canonicalnull
Boilerpipe Text
Last Updated : 17 Nov, 2025 Loops are a fundamental part of programming, and shell scripting is no exception. They allow you to automate repetitive tasks by running a block of code multiple times. You will use loops for all sorts of common tasks: for loop : Iterating over a list of items (e.g., files, servers, usernames). while loop : Running code as long as a condition is true (e.g., reading a file line by line). until loop : Running code until a condition becomes true (e.g., waiting for a file to appear). `while` statement in Shell Script in Linux The while loop is used when you don't know how many times to loop, but you know the condition to stay in the loop. How it works: It checks the condition. If it's TRUE, it runs the code, then checks again. It repeats until the condition is FALSE. #/bin/bash while <condition> do <command 1> <command 2> <etc> done [Statement]: Implementation of `While` Loop in Shell Script. First, we create a file using a text editor in Linux. In this case, we are using `vim`editor. vim looping.sh You can replace "looping.sh" with the desired name. Then we make our script executable using the `chmod` command in Linux. chmod +x looping.sh #/bin/bash a=0 # lt is less than operator #Iterate the loop until a less than 10 while [ $a -lt 10 ] do # Print the values echo $a # increment the value a=`expr $a + 1` done Output: While Loop in Linux Explanation: #/bin/bash : Specifies that the script should be interpreted using the Bash shell. a=0 : Initializes a variable a with the value 0. while [ $a -lt 10 ] : Initiates a while loop that continues as long as the value a is less than 10. do : Marks the beginning of the loop's body. echo $a : Prints the current value of a the console. a=expr $a + 1`` : Increments the value of a by 1. The expr command is used for arithmetic expressions. done : Marks the end of the loop `for` statement in Shell Script in Linux The for loop operates on lists of items. It repeats a set of commands for every item in a list.  Syntax:    #/bin/bash for <var> in <value1 value2 ... valuen> do <command 1> <command 2> <etc> done Here var is the name of a variable and word1 to wordN are sequences of characters separated by spaces (words). Each time the for loop executes, the value of the variable var is set to the next word in the list of words, word1 to wordN.  [Statement 1]: Implementation of `for` Loop with `break` statement in Shell Script. First, we create a file using a text editor in Linux. In this case, we are using `vim`editor. vim for.sh You can replace "for.sh" with the desired name. Then we make our script executable using the `chmod` command in Linux. chmod +x for.sh #/bin/bash #Start of for loop for a in 1 2 3 4 5 6 7 8 9 10 do # if a is equal to 5 break the loop if [ $a == 5 ] then break fi # Print the value echo "Iteration no $a" done Output: Explanation: #/bin/bash : Specifies that the script should be interpreted using the Bash shell. for a in 1 2 3 4 5 6 7 8 9 10 : Initiates a for loop that iterates over the values 1 through 10, assigning each value to the variable a in each iteration. do : Marks the beginning of the loop's body. if [ $a == 5 ] : Checks if the current value a is equal to 5. echo "Iteration no $a" : Prints a message indicating the current iteration number. done : Marks the end of the loop. The script sets up a for loop that iterates over the values 1 through 10. During each iteration, it checks if the value a is equal to 5. If it is, the loop is exited using the break statement. Otherwise, it prints a message indicating the current iteration number. The loop continues until it completes all iterations or until it encounters a break statement. [Statement 2]: Implementation of `for` Loop with `continue` statement in Shell Script. First, we create a file using a text editor in Linux. In this case, we are using `vim`editor. vim for_continue.sh You can replace "for_continue.sh" with the desired name. Then we make our script executable using the `chmod` command in Linux. chmod +x for_continue.sh #/bin/bash for a in 1 2 3 4 5 6 7 8 9 10 do # if a = 5 then continue the loop and # don't move to line 8 if [ $a == 5 ] then continue fi echo "Iteration no $a" done Output: continue statement in for loop in Linux Explanation: #/bin/bash: Specifies that the script should be interpreted using the Bash shell. for a in 1 2 3 4 5 6 7 8 9 10 : Initiates a for loop that iterates over the values 1 through 10, assigning each value to the variable a in each iteration. do : Marks the beginning of the loop's body. if [ $a == 5 ] : Checks if the current value a is equal to 5. echo "Iteration no $a" : Prints a message indicating the current iteration number. This line is skipped if a is equal to 5 due to the continue statement. done : Marks the end of the loop. The script sets up a for loop that iterates over the values 1 through 10. During each iteration, it checks if the value a is equal to 5. If it is, the loop continues to the next iteration without executing the remaining statements in the loop's body. Otherwise, it prints a message indicating the current iteration number. The loop continues until it completes all iterations. `until` statement in Shell Script in Linux The until loop is executed as many times as the condition/command evaluates to false. The loop terminates when the condition/command becomes true.  Syntax:   #/bin/bash until <condition> do <command 1> <command 2> <etc> done [Statement]: Implementing `until` Loop in Shell Script First, we create a file using a text editor in Linux. In this case, we are using `vim`editor.  vim until.sh You can replace "until. sh" with the desired name. Then we make our script executable using the `chmod` command in Linux. chmod +x until.sh #/bin/bash a=0 # -gt is greater than operator #Iterate the loop until a is greater than 10 until [ $a -gt 10 ] do # Print the values echo $a # increment the value a=`expr $a + 1` done Output: Explanation: #/bin/bash : Specifies that the script should be interpreted using the Bash shell. a=0 : Initializes a variable a with the value 0. until [ $a -gt 10 ] : Initiates a until loop that continues as long as the value a is not greater than 10. do : Marks the beginning of the loop's body. echo $a : Prints the current value of a the console. a=expr $a + 1`` : Increments the value of a by 1. The expr command is used for arithmetic expressions. done : Marks the end of the loop. Note: Shell scripting is a case-sensitive language, which means proper syntax has to be followed while writing the scripts. Examples of Looping Statements Below are some commonly used looping statements along with their basic structure and examples. [Example 1]: Iterating Over Colors Using a For Loop First, we create a file using a text editor in Linux. In this case, we are using `vim`editor.  vim color.sh You can replace "color.sh" with the desired name. Then we make our script executable using `chmod` command in Linux. chmod +x color.sh #/bin/bash COLORS="red green blue" # the for loop continues until it reads all the values from the COLORS for COLOR in $COLORS do echo "COLOR: $COLOR" done Output: For until in Linux Explanation: 1. Initialization of Colors: COLORS="red green blue": Initializes a variable named COLORS with a space-separated list of color values ("red", "green", and "blue"). 2. For Loop Iteration: for COLOR in $COLORS: Initiates a for loop that iterates over each value in the COLORS variable. 3. Loop Body: echo "COLOR: $COLOR": Prints a message for each color, displaying the current color value. The loop continues until it processes all the values present in the COLORS variable. This example demonstrates a simple for loop in Bash, iterating over a list of colors stored in the COLORS variable. The loop prints a message for each color, indicating the current color being processed. The loop iterates until all color values are exhausted. [Example 2]: Creating an Infinite Loop with "while true" in Shell Script First we create a file using a text editor in Linux. In this case we are using `vim`editor.  vim infinite.sh You can replace "infinite.sh" with desired name. Then we make our script executable using `chmod` command in Linux. chmod +x infinite.sh #/bin/bash while true do # Command to be executed # sleep 1 indicates it sleeps for 1 sec echo "Hi, I am infinity loop" sleep 1 done Output: infinite loop in linux Explanation: Infinite Loop Structure: 1. while true: Initiates a while loop that continues indefinitely as the condition true is always true. 2 . Loop Body: echo "Hi, I am infinity loop" : Prints a message indicating that the script is in an infinite loop. sleep 1 : Pauses the execution of the loop for 1 second before the next iteration. The loop continues indefinitely, executing the commands within its body repeatedly. This example showcases the creation of an infinite loop using the while true construct in Bash. The loop continuously prints a message indicating its status as an infinite loop and includes a sleep 1 command, causing a one-second delay between iterations. Infinite loops are often used for processes that need to run continuously until manually interrupted. [Example 3]: Interactive Name Confirmation Loop First we create a file using a text editor in Linux. In this case we are using `vim`editor.  vim name.sh You can replace "name.sh" with desired name. Then we make our script executable using `chmod` command in Linux. chmod +x name.sh #/bin/bash CORRECT=n while [ "$CORRECT" == "n" ] do # loop discontinues when you enter y i.e., when your name is correct # -p stands for prompt asking for the input read -p "Enter your name:" NAME read -p "Is ${NAME} correct? " CORRECT done Output: Interactive script in Linux Explanation: 1. Initialization: CORRECT=n: Initializes a variable named CORRECT with the value "n". This variable is used to control the loop. 2. Interactive Loop: while [ "$CORRECT" == "n" ]: Initiates a while loop that continues as long as the value of CORRECT is equal to "n". 3. Loop Body: read -p "Enter your name:" NAME: Prompts the user to enter their name and stores the input in the NAME variable. read -p "Is ${NAME} correct? " CORRECT: Asks the user to confirm if the entered name is correct and updates the CORRECT variable accordingly. The loop continues until the user enters "y" (indicating the correct name). This example demonstrates an interactive loop that prompts the user to enter their name and then asks for confirmation. The loop continues until the user confirms that the entered name is correct by inputting "y". The loop utilizes the `read` command for user input and updates the `CORRECT` variable to control the loop flow.
Markdown
[![geeksforgeeks](https://media.geeksforgeeks.org/gfg-gg-logo.svg)](https://www.geeksforgeeks.org/) ![search icon](https://media.geeksforgeeks.org/auth-dashboard-uploads/Property=Light---Default.svg) - Sign In - [Courses]() - [Tutorials]() - [Interview Prep]() - [Linux-Unix](https://www.geeksforgeeks.org/linux-unix/linux-tutorial/) - [Interview Questions](https://www.geeksforgeeks.org/linux-unix/linux-interview-questions/) - [Shell Scripting](https://www.geeksforgeeks.org/linux-unix/introduction-linux-shell-shell-scripting/) - [Kali](https://www.geeksforgeeks.org/linux-unix/introduction-to-kali-linux/) - [Ubuntu](https://www.geeksforgeeks.org/linux-unix/how-to-install-ubuntu-on-virtualbox/) - [Red Hat](https://www.geeksforgeeks.org/tag/red-hat/) - [CentOS](https://www.geeksforgeeks.org/linux-unix/getting-started-with-centos/) - [Docker](https://www.geeksforgeeks.org/devops/introduction-to-docker/) - [Kubernetes](https://www.geeksforgeeks.org/devops/introduction-to-kubernetes-k8s/) - [Python](https://www.geeksforgeeks.org/python/python-programming-language-tutorial/) # Looping Statements \| Shell Script Last Updated : 17 Nov, 2025 Loops are a fundamental part of programming, and shell scripting is no exception. They allow you to automate repetitive tasks by running a block of code multiple times. You will use loops for all sorts of common tasks: - ****for loop****: Iterating over a list of items (e.g., files, servers, usernames). - ****while loop****: Running code as long as a condition is true (e.g., reading a file line by line). - ****until loop****: Running code until a condition becomes true (e.g., waiting for a file to appear). ## \`while\` statement in Shell Script in Linux The while loop is used when you don't know how many times to loop, but you know the condition to stay in the loop. ### ****How it works:**** It checks the condition. If it's TRUE, it runs the code, then checks again. It repeats until the condition is FALSE. ``` #/bin/bash while <condition> do <command 1> <command 2> <etc> done ``` ### \[Statement\]: Implementation of \`While\` Loop in Shell Script. First, we create a file using a text editor in Linux. In this case, we are using \`vim\`editor. ``` vim looping.sh ``` - You can replace "looping.sh" with the desired name. - Then we make our script executable using the \`chmod\` command in Linux. ``` chmod +x looping.sh ``` ``` #/bin/bash a=0 # lt is less than operator #Iterate the loop until a less than 10 while [ $a -lt 10 ] do # Print the values echo $a # increment the value a=`expr $a + 1` done ``` ****Output:**** ![While Loop in Linux](https://media.geeksforgeeks.org/wp-content/uploads/20231122161714/99.png) While Loop in Linux ****Explanation:**** - ****\#/bin/bash****: Specifies that the script should be interpreted using the Bash shell. - ****a=0****: Initializes a variable a with the value 0. - ****while \[ \$a -lt 10 \]****: Initiates a while loop that continues as long as the value a is less than 10. - ****do****: Marks the beginning of the loop's body. - ****echo \$a****: Prints the current value of a the console. - ****a=expr \$a + 1\``****: Increments the value of a by 1. The expr command is used for arithmetic expressions. - ****done****: Marks the end of the loop ## \`for\` statement in Shell Script in Linux The for loop operates on lists of items. It repeats a set of commands for every item in a list. ****Syntax:**** ``` #/bin/bash for <var> in <value1 value2 ... valuen> do <command 1> <command 2> <etc> done ``` - Here var is the name of a variable and word1 to wordN are sequences of characters separated by spaces (words). Each time the for loop executes, the value of the variable var is set to the next word in the list of words, word1 to wordN. ### \[Statement 1\]: Implementation of \`for\` Loop with \`break\` statement in Shell Script. First, we create a file using a text editor in Linux. In this case, we are using \`vim\`editor. ``` vim for.sh ``` - You can replace "for.sh" with the desired name. - Then we make our script executable using the \`chmod\` command in Linux. ``` chmod +x for.sh ``` ``` #/bin/bash #Start of for loop for a in 1 2 3 4 5 6 7 8 9 10 do # if a is equal to 5 break the loop if [ $a == 5 ] then break fi # Print the value echo "Iteration no $a" done ``` ****Output:**** ![loop1](https://media.geeksforgeeks.org/wp-content/uploads/20251113153558725974/loop1.webp) ****Explanation:**** - ****\#/bin/bash****: Specifies that the script should be interpreted using the Bash shell. - ****for a in 1 2 3 4 5 6 7 8 9 10****: Initiates a for loop that iterates over the values 1 through 10, assigning each value to the variable a in each iteration. - ****do****: Marks the beginning of the loop's body. - ****if \[ \$a == 5 \]****: Checks if the current value a is equal to 5. - ****echo "Iteration no \$a"****: Prints a message indicating the current iteration number. - ****done****: Marks the end of the loop. The script sets up a for loop that iterates over the values 1 through 10. During each iteration, it checks if the value a is equal to 5. If it is, the loop is exited using the break statement. Otherwise, it prints a message indicating the current iteration number. The loop continues until it completes all iterations or until it encounters a break statement. ### \[Statement 2\]: Implementation of \`for\` Loop with \`continue\` statement in Shell Script. First, we create a file using a text editor in Linux. In this case, we are using \`vim\`editor. ``` vim for_continue.sh ``` - You can replace "for\_continue.sh" with the desired name. - Then we make our script executable using the \`chmod\` command in Linux. ``` chmod +x for_continue.sh ``` ``` #/bin/bash for a in 1 2 3 4 5 6 7 8 9 10 do # if a = 5 then continue the loop and # don't move to line 8 if [ $a == 5 ] then continue fi echo "Iteration no $a" done ``` ****Output:**** ![continue statement in for loop in Linux](https://media.geeksforgeeks.org/wp-content/uploads/20231122164109/101.png) continue statement in for loop in Linux ****Explanation:**** - ****\#/bin/bash:**** Specifies that the script should be interpreted using the Bash shell. - ****for a in 1 2 3 4 5 6 7 8 9 10****: Initiates a for loop that iterates over the values 1 through 10, assigning each value to the variable a in each iteration. - ****do****: Marks the beginning of the loop's body. - ****if \[ \$a == 5 \]****: Checks if the current value a is equal to 5. - ****echo "Iteration no \$a"****: Prints a message indicating the current iteration number. This line is skipped if a is equal to 5 due to the continue statement. - ****done****: Marks the end of the loop. The script sets up a for loop that iterates over the values 1 through 10. During each iteration, it checks if the value a is equal to 5. If it is, the loop continues to the next iteration without executing the remaining statements in the loop's body. Otherwise, it prints a message indicating the current iteration number. The loop continues until it completes all iterations. ## \`until\` statement in Shell Script in Linux The until loop is executed as many times as the condition/command evaluates to false. The loop terminates when the condition/command becomes true. ****Syntax:**** ``` #/bin/bash until <condition> do <command 1> <command 2> <etc> done ``` ### \[Statement\]: Implementing \`until\` Loop in Shell Script First, we create a file using a text editor in Linux. In this case, we are using \`vim\`editor. ``` vim until.sh ``` - You can replace "until. sh" with the desired name. - Then we make our script executable using the \`chmod\` command in Linux. ``` chmod +x until.sh ``` ``` #/bin/bash a=0 # -gt is greater than operator #Iterate the loop until a is greater than 10 until [ $a -gt 10 ] do # Print the values echo $a # increment the value a=`expr $a + 1` done ``` ****Output:**** ![loop2](https://media.geeksforgeeks.org/wp-content/uploads/20251113153558935764/loop2.webp) ****Explanation:**** - ****\#/bin/bash****: Specifies that the script should be interpreted using the Bash shell. - ****a=0****: Initializes a variable a with the value 0. - ****until \[ \$a -gt 10 \]****: Initiates a until loop that continues as long as the value a is not greater than 10. - ****do****: Marks the beginning of the loop's body. - ****echo \$a****: Prints the current value of a the console. - ****a=expr \$a + 1\``****: Increments the value of a by 1. The expr command is used for arithmetic expressions. - ****done****: Marks the end of the loop. ****Note:**** Shell scripting is a case-sensitive language, which means proper syntax has to be followed while writing the scripts. ## Examples of Looping Statements Below are some commonly used looping statements along with their basic structure and examples. ### \[Example 1\]: Iterating Over Colors Using a For Loop First, we create a file using a text editor in Linux. In this case, we are using \`vim\`editor. ``` vim color.sh ``` - You can replace "color.sh" with the desired name. - Then we make our script executable using \`chmod\` command in Linux. ``` chmod +x color.sh ``` ``` #/bin/bash COLORS="red green blue" # the for loop continues until it reads all the values from the COLORS for COLOR in $COLORS do echo "COLOR: $COLOR" done ``` ****Output:**** ![For until in Linux](https://media.geeksforgeeks.org/wp-content/uploads/20231122171459/103.png) For until in Linux ****Explanation:**** ****1\. Initialization of Colors:**** - COLORS="red green blue": Initializes a variable named COLORS with a space-separated list of color values ("red", "green", and "blue"). ****2\. For Loop Iteration:**** - for COLOR in \$COLORS: Initiates a for loop that iterates over each value in the COLORS variable. ****3\. Loop Body:**** - echo "COLOR: \$COLOR": Prints a message for each color, displaying the current color value. - The loop continues until it processes all the values present in the COLORS variable. This example demonstrates a simple for loop in Bash, iterating over a list of colors stored in the COLORS variable. The loop prints a message for each color, indicating the current color being processed. The loop iterates until all color values are exhausted. ### \[Example 2\]: Creating an Infinite Loop with "while true" in Shell Script First we create a file using a text editor in Linux. In this case we are using \`vim\`editor. ``` vim infinite.sh ``` - You can replace "infinite.sh" with desired name. - Then we make our script executable using \`chmod\` command in Linux. ``` chmod +x infinite.sh ``` ``` #/bin/bash while true do # Command to be executed # sleep 1 indicates it sleeps for 1 sec echo "Hi, I am infinity loop" sleep 1 done ``` ****Output:**** ![infinite loop in linux](https://media.geeksforgeeks.org/wp-content/uploads/20231122172917/104.png) infinite loop in linux ****Explanation:**** ****Infinite Loop Structure:**** ****1\.**** ****while true:**** Initiates a while loop that continues indefinitely as the condition true is always true. ****2****. ****Loop Body:**** - ****echo "Hi, I am infinity loop"****: Prints a message indicating that the script is in an infinite loop. - ****sleep 1****: Pauses the execution of the loop for 1 second before the next iteration. - The loop continues indefinitely, executing the commands within its body repeatedly. This example showcases the creation of an infinite loop using the while true construct in Bash. The loop continuously prints a message indicating its status as an infinite loop and includes a sleep 1 command, causing a one-second delay between iterations. Infinite loops are often used for processes that need to run continuously until manually interrupted. ### \[Example 3\]: Interactive Name Confirmation Loop First we create a file using a text editor in Linux. In this case we are using \`vim\`editor. ``` vim name.sh ``` - You can replace "name.sh" with desired name. - Then we make our script executable using \`chmod\` command in Linux. ``` chmod +x name.sh ``` ``` #/bin/bash CORRECT=n while [ "$CORRECT" == "n" ] do # loop discontinues when you enter y i.e., when your name is correct # -p stands for prompt asking for the input read -p "Enter your name:" NAME read -p "Is ${NAME} correct? " CORRECT done ``` ****Output:**** ![Interactive script in Linux](https://media.geeksforgeeks.org/wp-content/uploads/20231122173840/105.png) Interactive script in Linux #### Explanation: ****1\. Initialization:**** - CORRECT=n: Initializes a variable named CORRECT with the value "n". This variable is used to control the loop. ****2\. Interactive Loop:**** - while \[ "\$CORRECT" == "n" \]: Initiates a while loop that continues as long as the value of CORRECT is equal to "n". ****3\. Loop Body:**** - read -p "Enter your name:" NAME: Prompts the user to enter their name and stores the input in the NAME variable. - read -p "Is \${NAME} correct? " CORRECT: Asks the user to confirm if the entered name is correct and updates the CORRECT variable accordingly. - The loop continues until the user enters "y" (indicating the correct name). This example demonstrates an interactive loop that prompts the user to enter their name and then asks for confirmation. The loop continues until the user confirms that the entered name is correct by inputting "y". The loop utilizes the \`read\` command for user input and updates the \`CORRECT\` variable to control the loop flow. Comment [B](https://www.geeksforgeeks.org/user/bilal-hungund/) [bilal-hungund](https://www.geeksforgeeks.org/user/bilal-hungund/) 28 Article Tags: Article Tags: [Misc](https://www.geeksforgeeks.org/category/misc/) [Linux-Unix](https://www.geeksforgeeks.org/category/linux-unix/) [Shell](https://www.geeksforgeeks.org/tag/shell/) [Shell Script](https://www.geeksforgeeks.org/tag/shell-script/) ### Explore [![GeeksforGeeks](https://media.geeksforgeeks.org/auth-dashboard-uploads/gfgFooterLogo.png)](https://www.geeksforgeeks.org/) ![location](https://media.geeksforgeeks.org/img-practice/Location-1685004904.svg) Corporate & Communications Address: A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305) ![location](https://media.geeksforgeeks.org/img-practice/Location-1685004904.svg) Registered Address: K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305 [![GFG App on Play Store](https://media.geeksforgeeks.org/auth-dashboard-uploads/googleplay-%281%29.png)](https://geeksforgeeksapp.page.link/gfg-app)[![GFG App on App Store](https://media.geeksforgeeks.org/auth-dashboard-uploads/appstore-%281%29.png)](https://geeksforgeeksapp.page.link/gfg-app) - Company - [About Us](https://www.geeksforgeeks.org/about/) - [Legal](https://www.geeksforgeeks.org/legal/) - [Privacy Policy](https://www.geeksforgeeks.org/legal/privacy-policy/) - [Contact Us](https://www.geeksforgeeks.org/about/contact-us/) - [Advertise with us](https://www.geeksforgeeks.org/advertise-with-us/) - [GFG Corporate Solution](https://www.geeksforgeeks.org/gfg-corporate-solution/) - [Campus Training Program](https://www.geeksforgeeks.org/campus-training-program/) - Explore - [POTD](https://www.geeksforgeeks.org/problem-of-the-day) - [Job-A-Thon](https://practice.geeksforgeeks.org/events/rec/job-a-thon/) - [Blogs](https://www.geeksforgeeks.org/category/blogs/?type=recent) - [Nation Skill Up](https://www.geeksforgeeks.org/nation-skill-up/) - Tutorials - [Programming Languages](https://www.geeksforgeeks.org/computer-science-fundamentals/programming-language-tutorials/) - [DSA](https://www.geeksforgeeks.org/dsa/dsa-tutorial-learn-data-structures-and-algorithms/) - [Web Technology](https://www.geeksforgeeks.org/web-tech/web-technology/) - [AI, ML & Data Science](https://www.geeksforgeeks.org/machine-learning/ai-ml-and-data-science-tutorial-learn-ai-ml-and-data-science/) - [DevOps](https://www.geeksforgeeks.org/devops/devops-tutorial/) - [CS Core Subjects](https://www.geeksforgeeks.org/gate/gate-exam-tutorial/) - [Interview Preparation](https://www.geeksforgeeks.org/aptitude/interview-corner/) - [Software and Tools](https://www.geeksforgeeks.org/websites-apps/software-and-tools-a-to-z-list/) - Courses - [ML and Data Science](https://www.geeksforgeeks.org/courses/category/machine-learning-data-science) - [DSA and Placements](https://www.geeksforgeeks.org/courses/category/dsa-placements) - [Web Development](https://www.geeksforgeeks.org/courses/category/development-testing) - [Programming Languages](https://www.geeksforgeeks.org/courses/category/programming-languages) - [DevOps & Cloud](https://www.geeksforgeeks.org/courses/category/cloud-devops) - [GATE](https://www.geeksforgeeks.org/courses/category/gate) - [Trending Technologies](https://www.geeksforgeeks.org/courses/category/trending-technologies/) - Videos - [DSA](https://www.geeksforgeeks.org/videos/category/sde-sheet/) - [Python](https://www.geeksforgeeks.org/videos/category/python/) - [Java](https://www.geeksforgeeks.org/videos/category/java-w6y5f4/) - [C++](https://www.geeksforgeeks.org/videos/category/c/) - [Web Development](https://www.geeksforgeeks.org/videos/category/web-development/) - [Data Science](https://www.geeksforgeeks.org/videos/category/data-science/) - [CS Subjects](https://www.geeksforgeeks.org/videos/category/cs-subjects/) - Preparation Corner - [Interview Corner](https://www.geeksforgeeks.org/interview-prep/interview-corner/) - [Aptitude](https://www.geeksforgeeks.org/aptitude/aptitude-questions-and-answers/) - [Puzzles](https://www.geeksforgeeks.org/aptitude/puzzles/) - [GfG 160](https://www.geeksforgeeks.org/courses/gfg-160-series) - [System Design](https://www.geeksforgeeks.org/system-design/system-design-tutorial/) [@GeeksforGeeks, Sanchhaya Education Private Limited](https://www.geeksforgeeks.org/), [All rights reserved](https://www.geeksforgeeks.org/copyright-information/) ![]()
Readable Markdown
Last Updated : 17 Nov, 2025 Loops are a fundamental part of programming, and shell scripting is no exception. They allow you to automate repetitive tasks by running a block of code multiple times. You will use loops for all sorts of common tasks: - ****for loop****: Iterating over a list of items (e.g., files, servers, usernames). - ****while loop****: Running code as long as a condition is true (e.g., reading a file line by line). - ****until loop****: Running code until a condition becomes true (e.g., waiting for a file to appear). ## \`while\` statement in Shell Script in Linux The while loop is used when you don't know how many times to loop, but you know the condition to stay in the loop. ### ****How it works:**** It checks the condition. If it's TRUE, it runs the code, then checks again. It repeats until the condition is FALSE. ``` #/bin/bash while <condition> do <command 1> <command 2> <etc> done ``` ### \[Statement\]: Implementation of \`While\` Loop in Shell Script. First, we create a file using a text editor in Linux. In this case, we are using \`vim\`editor. ``` vim looping.sh ``` - You can replace "looping.sh" with the desired name. - Then we make our script executable using the \`chmod\` command in Linux. ``` chmod +x looping.sh ``` ``` #/bin/bash a=0 # lt is less than operator #Iterate the loop until a less than 10 while [ $a -lt 10 ] do # Print the values echo $a # increment the value a=`expr $a + 1` done ``` ****Output:**** ![While Loop in Linux](https://media.geeksforgeeks.org/wp-content/uploads/20231122161714/99.png) While Loop in Linux ****Explanation:**** - ****\#/bin/bash****: Specifies that the script should be interpreted using the Bash shell. - ****a=0****: Initializes a variable a with the value 0. - ****while \[ \$a -lt 10 \]****: Initiates a while loop that continues as long as the value a is less than 10. - ****do****: Marks the beginning of the loop's body. - ****echo \$a****: Prints the current value of a the console. - ****a=expr \$a + 1\``****: Increments the value of a by 1. The expr command is used for arithmetic expressions. - ****done****: Marks the end of the loop ## \`for\` statement in Shell Script in Linux The for loop operates on lists of items. It repeats a set of commands for every item in a list. ****Syntax:**** ``` #/bin/bash for <var> in <value1 value2 ... valuen> do <command 1> <command 2> <etc> done ``` - Here var is the name of a variable and word1 to wordN are sequences of characters separated by spaces (words). Each time the for loop executes, the value of the variable var is set to the next word in the list of words, word1 to wordN. ### \[Statement 1\]: Implementation of \`for\` Loop with \`break\` statement in Shell Script. First, we create a file using a text editor in Linux. In this case, we are using \`vim\`editor. ``` vim for.sh ``` - You can replace "for.sh" with the desired name. - Then we make our script executable using the \`chmod\` command in Linux. ``` chmod +x for.sh ``` ``` #/bin/bash #Start of for loop for a in 1 2 3 4 5 6 7 8 9 10 do # if a is equal to 5 break the loop if [ $a == 5 ] then break fi # Print the value echo "Iteration no $a" done ``` ****Output:**** ![loop1](https://media.geeksforgeeks.org/wp-content/uploads/20251113153558725974/loop1.webp) ****Explanation:**** - ****\#/bin/bash****: Specifies that the script should be interpreted using the Bash shell. - ****for a in 1 2 3 4 5 6 7 8 9 10****: Initiates a for loop that iterates over the values 1 through 10, assigning each value to the variable a in each iteration. - ****do****: Marks the beginning of the loop's body. - ****if \[ \$a == 5 \]****: Checks if the current value a is equal to 5. - ****echo "Iteration no \$a"****: Prints a message indicating the current iteration number. - ****done****: Marks the end of the loop. The script sets up a for loop that iterates over the values 1 through 10. During each iteration, it checks if the value a is equal to 5. If it is, the loop is exited using the break statement. Otherwise, it prints a message indicating the current iteration number. The loop continues until it completes all iterations or until it encounters a break statement. ### \[Statement 2\]: Implementation of \`for\` Loop with \`continue\` statement in Shell Script. First, we create a file using a text editor in Linux. In this case, we are using \`vim\`editor. ``` vim for_continue.sh ``` - You can replace "for\_continue.sh" with the desired name. - Then we make our script executable using the \`chmod\` command in Linux. ``` chmod +x for_continue.sh ``` ``` #/bin/bash for a in 1 2 3 4 5 6 7 8 9 10 do # if a = 5 then continue the loop and # don't move to line 8 if [ $a == 5 ] then continue fi echo "Iteration no $a" done ``` ****Output:**** ![continue statement in for loop in Linux](https://media.geeksforgeeks.org/wp-content/uploads/20231122164109/101.png) continue statement in for loop in Linux ****Explanation:**** - ****\#/bin/bash:**** Specifies that the script should be interpreted using the Bash shell. - ****for a in 1 2 3 4 5 6 7 8 9 10****: Initiates a for loop that iterates over the values 1 through 10, assigning each value to the variable a in each iteration. - ****do****: Marks the beginning of the loop's body. - ****if \[ \$a == 5 \]****: Checks if the current value a is equal to 5. - ****echo "Iteration no \$a"****: Prints a message indicating the current iteration number. This line is skipped if a is equal to 5 due to the continue statement. - ****done****: Marks the end of the loop. The script sets up a for loop that iterates over the values 1 through 10. During each iteration, it checks if the value a is equal to 5. If it is, the loop continues to the next iteration without executing the remaining statements in the loop's body. Otherwise, it prints a message indicating the current iteration number. The loop continues until it completes all iterations. ## \`until\` statement in Shell Script in Linux The until loop is executed as many times as the condition/command evaluates to false. The loop terminates when the condition/command becomes true. ****Syntax:**** ``` #/bin/bash until <condition> do <command 1> <command 2> <etc> done ``` ### \[Statement\]: Implementing \`until\` Loop in Shell Script First, we create a file using a text editor in Linux. In this case, we are using \`vim\`editor. ``` vim until.sh ``` - You can replace "until. sh" with the desired name. - Then we make our script executable using the \`chmod\` command in Linux. ``` chmod +x until.sh ``` ``` #/bin/bash a=0 # -gt is greater than operator #Iterate the loop until a is greater than 10 until [ $a -gt 10 ] do # Print the values echo $a # increment the value a=`expr $a + 1` done ``` ****Output:**** ![loop2](https://media.geeksforgeeks.org/wp-content/uploads/20251113153558935764/loop2.webp) ****Explanation:**** - ****\#/bin/bash****: Specifies that the script should be interpreted using the Bash shell. - ****a=0****: Initializes a variable a with the value 0. - ****until \[ \$a -gt 10 \]****: Initiates a until loop that continues as long as the value a is not greater than 10. - ****do****: Marks the beginning of the loop's body. - ****echo \$a****: Prints the current value of a the console. - ****a=expr \$a + 1\``****: Increments the value of a by 1. The expr command is used for arithmetic expressions. - ****done****: Marks the end of the loop. ****Note:**** Shell scripting is a case-sensitive language, which means proper syntax has to be followed while writing the scripts. ## Examples of Looping Statements Below are some commonly used looping statements along with their basic structure and examples. ### \[Example 1\]: Iterating Over Colors Using a For Loop First, we create a file using a text editor in Linux. In this case, we are using \`vim\`editor. ``` vim color.sh ``` - You can replace "color.sh" with the desired name. - Then we make our script executable using \`chmod\` command in Linux. ``` chmod +x color.sh ``` ``` #/bin/bash COLORS="red green blue" # the for loop continues until it reads all the values from the COLORS for COLOR in $COLORS do echo "COLOR: $COLOR" done ``` ****Output:**** ![For until in Linux](https://media.geeksforgeeks.org/wp-content/uploads/20231122171459/103.png) For until in Linux ****Explanation:**** ****1\. Initialization of Colors:**** - COLORS="red green blue": Initializes a variable named COLORS with a space-separated list of color values ("red", "green", and "blue"). ****2\. For Loop Iteration:**** - for COLOR in \$COLORS: Initiates a for loop that iterates over each value in the COLORS variable. ****3\. Loop Body:**** - echo "COLOR: \$COLOR": Prints a message for each color, displaying the current color value. - The loop continues until it processes all the values present in the COLORS variable. This example demonstrates a simple for loop in Bash, iterating over a list of colors stored in the COLORS variable. The loop prints a message for each color, indicating the current color being processed. The loop iterates until all color values are exhausted. ### \[Example 2\]: Creating an Infinite Loop with "while true" in Shell Script First we create a file using a text editor in Linux. In this case we are using \`vim\`editor. ``` vim infinite.sh ``` - You can replace "infinite.sh" with desired name. - Then we make our script executable using \`chmod\` command in Linux. ``` chmod +x infinite.sh ``` ``` #/bin/bash while true do # Command to be executed # sleep 1 indicates it sleeps for 1 sec echo "Hi, I am infinity loop" sleep 1 done ``` ****Output:**** ![infinite loop in linux](https://media.geeksforgeeks.org/wp-content/uploads/20231122172917/104.png) infinite loop in linux ****Explanation:**** ****Infinite Loop Structure:**** ****1\.**** ****while true:**** Initiates a while loop that continues indefinitely as the condition true is always true. ****2****. ****Loop Body:**** - ****echo "Hi, I am infinity loop"****: Prints a message indicating that the script is in an infinite loop. - ****sleep 1****: Pauses the execution of the loop for 1 second before the next iteration. - The loop continues indefinitely, executing the commands within its body repeatedly. This example showcases the creation of an infinite loop using the while true construct in Bash. The loop continuously prints a message indicating its status as an infinite loop and includes a sleep 1 command, causing a one-second delay between iterations. Infinite loops are often used for processes that need to run continuously until manually interrupted. ### \[Example 3\]: Interactive Name Confirmation Loop First we create a file using a text editor in Linux. In this case we are using \`vim\`editor. ``` vim name.sh ``` - You can replace "name.sh" with desired name. - Then we make our script executable using \`chmod\` command in Linux. ``` chmod +x name.sh ``` ``` #/bin/bash CORRECT=n while [ "$CORRECT" == "n" ] do # loop discontinues when you enter y i.e., when your name is correct # -p stands for prompt asking for the input read -p "Enter your name:" NAME read -p "Is ${NAME} correct? " CORRECT done ``` ****Output:**** ![Interactive script in Linux](https://media.geeksforgeeks.org/wp-content/uploads/20231122173840/105.png) Interactive script in Linux #### Explanation: ****1\. Initialization:**** - CORRECT=n: Initializes a variable named CORRECT with the value "n". This variable is used to control the loop. ****2\. Interactive Loop:**** - while \[ "\$CORRECT" == "n" \]: Initiates a while loop that continues as long as the value of CORRECT is equal to "n". ****3\. Loop Body:**** - read -p "Enter your name:" NAME: Prompts the user to enter their name and stores the input in the NAME variable. - read -p "Is \${NAME} correct? " CORRECT: Asks the user to confirm if the entered name is correct and updates the CORRECT variable accordingly. - The loop continues until the user enters "y" (indicating the correct name). This example demonstrates an interactive loop that prompts the user to enter their name and then asks for confirmation. The loop continues until the user confirms that the entered name is correct by inputting "y". The loop utilizes the \`read\` command for user input and updates the \`CORRECT\` variable to control the loop flow.
Shard103 (laksa)
Root Hash12046344915360636903
Unparsed URLorg,geeksforgeeks!www,/linux-unix/looping-statements-shell-script/ s443