Facebook Twitter Instagram
    TecAdmin
    • Home
    • Ubuntu 20.04
      • Upgrade Ubuntu
      • Install Java
      • Install Node.js
      • Install Docker
      • Install LAMP Stack
    • Tutorials
      • AWS
      • Shell Scripting
      • Docker
      • Git
      • MongoDB
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    Home»Programming»Bash Shell»Create A Infinite Loop in Shell Script

    Create A Infinite Loop in Shell Script

    RahulBy RahulAugust 20, 20201 Min Read

    Question – How do I create an infinite loop in shell script on Unix/Linux operating system?

    An infinite loop is used for running a set of instruction with never ending repeat. In this we create a loop which runs endlessly and keep executing the instructions until force stopped externally.

    Bash Infinite While Loop

    In this scenario, which loop is the best option. The following syntax is used for create infinite while loop in a shell script.

    Loop Example:

    1
    2
    3
    4
    5
    6
    7
    8
    #!/usr/bin/env bash
     
    while :
    do
        echo "Press [CTRL+C] to exit this loop..."
        # Add more instructions here
        sleep 2
    done

    You can also Unix true command with while loop to run it endlessly. The while loop syntax with true command will look like below example.

    Loop Example:

    1
    2
    3
    4
    5
    6
    7
    8
    #!/usr/bin/env bash
     
    while true
    do
        echo "Press [CTRL+C] to exit this loop..."
        # Add more instructions here
        sleep 2
    done

    Add Exist Instruction in Infinite Loop

    Sometimes you may need to exit from a never ending loop based on a condition. If a specific condition meet and your want that infinite loop should break on that condition.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    #!/usr/bin/env bash
     
    while true
    do
        echo "Press [CTRL+C] to exit this loop..."
        # Add more instructions here
        sleep 2
     
       if [ condition ]
       then
           break
       fi
    done

    Add a if statement in above loop to break on matching condition.

    bash loop while
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp
    Previous ArticleHow to Install Sublime Text 3 on Ubuntu 20.04
    Next Article How to find files larger than 10MB, 100MB, 1GB in Linux

    Related Posts

    How to Backup Website to Amazon S3 using Shell Script

    Updated:March 24, 20222 Mins Read

    Bash Printf Command

    Updated:December 23, 20212 Mins Read

    Bash Sequence Expression (Define Range)

    Updated:January 12, 20222 Mins Read

    Creating Menu in Shell Script (Linux Select Command)

    Updated:May 31, 20222 Mins Read

    Bash Break and Continue

    Updated:November 9, 20213 Mins Read

    Bash String Concatenate

    Updated:January 5, 20222 Mins Read

    Leave A Reply Cancel Reply

    Recent Posts
    • Filesystem Hierarchy Structure (FHS) in Linux
    • How to accept user input in Python
    • What is difference between var, let and const in JavaScript?
    • What is CPU? – Definition, Types and Parts
    • What is the /etc/aliases file
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.