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»Creating Menu in Shell Script (Linux Select Command)

    Creating Menu in Shell Script (Linux Select Command)

    RahulBy RahulDecember 10, 20212 Mins ReadUpdated:May 31, 2022

    Bash Select construct is used to create a numbered menu from the list of items. That is helpful for shell scripts that required user selection across multiple items.

    Syntax

    The select statement has a similar syntax as ‘for loop’ and it is:

    select ITEM in [List]
    do
         [commands]
    done
    

    Here the List can be an array, a range of numbers, a series of strings separated by space, the output of a command, etc. And when the select construct will be invoked, each item from the list will be printed with a numbered sequence. The construct will continue to run until the break command is executed.

    Bash Select Example

    Let’s understand the select construct with an example. Here we have created a bash script named brand.sh and the select command is used to retrieve the data from the list as a menu. The script will first print the name of all the brands in the list and then it will ask the user to choose any one of them and it will print the name of the selected brand.

    1
    2
    3
    4
    5
    6
    #!/bin/bash
     
    select brand in Apple Google Microsoft Amazon Meta
    do
      echo "You have chosen $brand"
    done

    Run the script with ‘bash brand.sh’. You will see the following output.

    Output
    1) Apple 2) Google 3) Microsoft 4) Amazon 5) Meta #? 1 You have chosen Apple #? 3 You have chosen Microsoft #? ^C

    Press CTRL+C to exit.

    One More Example

    Let’s take another example of the select construct to see how it works with a case statement.

    Here we will create a new file named select.sh and once we will run the file, the user will select any item, and then the case statement will match the item with the case value. If no value is matched then ‘Invalid entry’ will print.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    #!/bin/bash
     
    echo "Which Operating System do you like?"
     
    select os in Ubuntu LinuxMint Windows8 Windows7 WindowsXP
    do
    case $os in
      "Ubuntu"|"LinuxMint")
         echo "I also use $os."
         ;;
      "Windows8" | "Windows10" | "WindowsXP")
         echo "Why don't you try Linux?"
       ;;
    *)
    echo "Invalid entry."
    break
    ;;
    esac
    done

    Now run the script with bash select.sh and you will see the following output.

    Output
    1) Ubuntu 3) Fedora 5) Windows7 2) LinuxMint 4) Windows8 6) WindowsXP #? 1 I also use Ubuntu. #? 2 I also use LinuxMint. #? 4 Why don't you try Linux? #? 7 Invalid entry.

    Conclusion

    This guide explains to use of the select command in bash scripting.

    bash select
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp
    Previous Article(Resolved) -bash: /bin/mv: Argument list too long
    Next Article Bash Sequence Expression (Define Range)

    Related Posts

    How to Correctly Set the $PATH variable in Bash

    2 Mins Read

    How to run a command on bash script exits

    1 Min Read

    Convert String to Lowercase in Bash – Easier Than You Think

    Updated:August 1, 20221 Min Read

    How to run a command or function on error in Bash Script

    Updated:July 30, 20223 Mins Read

    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

    3 Comments

    1. Jason Daigo on May 30, 2022 12:43 pm

      these scripts dont seem to work on debian or ubuntu. on arch they work, i get “select: not found” and “Syntax error: “do” unexpected”

      Reply
      • Rahul on May 31, 2022 6:36 am

        Hi Jason, Make sure you are using the “bash” command to run the script not “sh”.

        $ bash brands.sh
        

        or

        $ chmod +x brands.sh
        $ ./brands.sh
        
        Reply
    2. Indrajeet on December 29, 2021 10:44 am

      This information was needful.

      Reply

    Leave A Reply Cancel Reply

    Recent Posts
    • How To Install Google Chrome On macOS
    • How to Install Minecraft on Ubuntu 22.04 & 20.04
    • Running a Cron job every Sunday (Weekly)
    • Running Multiple Commands At Once in Linux
    • What are the Access Modifiers in Java
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

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