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»General Articles»Bash Sequence Expression (Define Range)

    Bash Sequence Expression (Define Range)

    RahulBy RahulDecember 14, 20212 Mins ReadUpdated:January 12, 2022

    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
    Previous ArticleBash Select Command (Create Menu in Shell Scripts)
    Next Article 10 Best Linux Video Players in 2022

    Related Posts

    How to Find Django Install Location in Linux

    Updated:April 27, 20221 Min Read

    (Resolved) – ReactJS 404 Error on Page Reload

    2 Mins Read

    Adding a New SSH Key in GitHub

    Updated:April 1, 20223 Mins Read

    How to Backup Website to Amazon S3 using Shell Script

    Updated:March 24, 20222 Mins Read

    13 Best Linux Terminal Emulators and Bash Editors

    8 Mins Read

    How To Install Oracle VirtualBox on Debian 11

    2 Mins Read

    Leave A Reply Cancel Reply

    Recent Posts
    • How to Install JAVA on Ubuntu 22.04
    • Switching Display Manager in Ubuntu – GDM, LightDM & SDDM
    • Changing the Login Screen Background in Ubuntu 22.04 & 20.04
    • How To Install PHP (8.1, 7.4 or 5.6) on Ubuntu 22.04
    • (Resolved) Please install all available updates for your release before upgrading
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

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