Facebook X (Twitter) Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook X (Twitter) Instagram
    TecAdmin
    You are at:Home»General Articles»Bash Sequence Expression (Define Range)

    Bash Sequence Expression (Define Range)

    By RahulJanuary 12, 20222 Mins Read

    The Sequence Expression is used to create a range of characters and integers by defining a start and endpoint. Usually, the Bash Sequence Expression is used with For Loops.

    The syntax of Sequence Expression is:

    {START..END[..INCREMENT]}
    

    Here the start and end values are mandatory and can be either characters or integers. Next, the increment value is optional and if we use it then it must be separated from the End value with two dots. If we do not use an increment value then the default value would be 1.

    Sequence Expression Examples in Bash

    Let’s take some examples of printing the sequence values in bash shell. We also include examples of defining range with loops in shell scripting.

    1. Let’s begin with simple example. Open a terminal and execute:
      echo {0..5}
      
      Output
      0 1 2 3 4 5
    2. You can also use the alphabets in a range.
      echo {a..e}
      
      Output
      a b c d e
    3. If the Start value is greater than the End value then there will be a decrement in the range.

      for i in {5..0}
      do 
       echo “No: $i”
      done 
      
      Output
      No: 5 No: 4 No: 3 No: 2 No: 1
    4. You can overwrite the default increment value (1) with custom value. Set the increment value to 5 instead of default value, write the syntax like:
      for i in {0..20..5}
      do 
         echo “No: $i”
      done
      
      Output
      No: 0 No: 5 No: 10 No: 15 No: 20
    5. We can also prefix and suffix the expression with other values.
      echo A{0..5}B
      
      Output
      A0B A1B A2B A3B A4B A5B
    6. You can also add a leading 0 in front of each integer to make them of the same length.
      for i in {00..5}
      do 
         echo "No: $i"
      done
      
      Output
      No: 00 No: 01 No: 02 No: 03 No: 04 No: 05
    7. We can also use seq Command to print a sequence. For example:
      seq 1 5
      
      Output
      1 2 3 4 5
    8. You can also define the default increment value with the seq command. The increment value is defined in the middle of start and end value. For example:
      seq 1 2 10
      
      Output
      1 3 5 7 9
    9. echo "Even Numbers:" $(seq 0 2 10)
      
      Output
      Even Numbers: 0 2 4 6 8 10

    Conclusion

    In this tutorial, You have learned to create or print the sequences in bash shell. It also helped you to define a specific range with loops.

    bash expression range
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    Difference Between Full Virtualization vs Paravirtualization

    Virtualization vs. Containerization: A Comparative Analysis

    Using .env File in FastAPI

    Add A Comment

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • Difference Between Full Virtualization vs Paravirtualization
    • Virtualization vs. Containerization: A Comparative Analysis
    • Using .env Files in Django
    • Using .env File in FastAPI
    • Setting Up Email Notifications for Django Error Reporting
    Facebook X (Twitter) Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

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