Logo
  • Ubuntu
  • CentOS
  • Debian
  • Fedora
  • RedHat

Bash Select (Make Menus) - DesignLinux

Nov 24 2020
designlinux 0 Comments
bash select

Using the select construct you can generate menus. In this guide, we will discuss about the basics of the select construct in Bash.

Bash select Construct#

You can generate a menu from the list of items using the select construct. Following is the syntax for the select construct and it is similar to the for loop syntax:

select ITEM in [LIST]
do
  [COMMANDS]
done

Here, in [LIST] you can pass a range of numbers, an array, series of strings separated by space, output of a command, etc. Using the PS3 environment variable you can set a custom prompt for the select construct.

Each item from the list will be printed on the screen when the select construct is invoked. If the entered number will matched with the number of the displayed items then it will set the value to that item. The variable REPLY holds the value of the selected item. If the user input is empty, the prompt and the menu list are displayed again.

The select loop will continue to run and prompt for user input until the break command is executed.

For better understanding let’s see an example of select construct:

PS3="Input a number: "

select state in Florida California Texas Alaska
do
    echo "Selected state: $state"
    echo "Selected number: $REPLY"
done

This script will show a menu with the list of items with the accompanying number and the PS3 prompt. Once the user enter any number the selected state and the number will be print on screen.

1) Florida
2) California
3) Texas
4) Alaska

Enter a number: 2
Selected character: California
Selected number: 2
Enter a number:

Bash select Example#

Generally, the select is used with the case or if statements. Below is given simple example for better understanding.

It will do simple arithmetic operations on basis of user inputs.

PS3="Select the arithmetic operation: "

select opt in addition subtraction multiplication division quit; do

  case $opt in
    addition)
      read -p "First number: " n1
      read -p "Second number: " n2
      echo "$n1 + $n2 = $(($n1+$n2))"
      ;;
    subtraction)
      read -p "First number: " n1
      read -p "Second number: " n2
      echo "$n1 - $n2 = $(($n1-$n2))"
      ;;
    multiplication)
      read -p "First number: " n1
      read -p "Second number: " n2
      echo "$n1 * $n2 = $(($n1*$n2))"
      ;;
    division)
      read -p "First number: " n1
      read -p "Second number: " n2
      echo "$n1 / $n2 = $(($n1/$n2))"
      ;;
    quit)
      break
      ;;
    *) 
      echo "Invalid option $REPLY"
      ;;
  esac
done

In output it will show the menu and PS3 prompt for user input. User have to select the operation and enter two numbers for perform operation. On the base of user selection the script will echo the result. It will also ask for the new operation after each selection until the break command is executed.

1) addition
2) subtraction
3) multiplication
4) division
5) quit
Select the arithmetic operation: 1
First number: 2
Second number: 4
2 + 4 = 6
Select the operation: 3
First number: 4
Second number: 5
4 * 5 = 20
Select the operation: 7
Invalid option 7
Select the operation: 

The disadvantage of this script is that it will work only with the integers.

We can also make the above example with more logic and reducing of codes by making function. We will use the bc tools which supports the floating numbers.

calculation () {
  read -p "First number: " n1
  read -p "Second number: " n2
  echo "$n1 $1 $n2 = " $(bc -l <<< "$n1$1$n2")
}

PS3="Select the arithmetic operation: "

select opt in addition subtraction multiplication division quit; do

  case $opt in
    addition)
      calculate "+";;
    subtraction)
      calculate "-";;
    multiplication)
      calculate "*";;
    division)
      calculate "/";;
    quit)
      break;;
    *) 
      echo "Invalid option $REPLY";;
  esac
done
1) addition
2) subtraction
3) multiplication
4) division
5) quit
Select the arithmetic operation: 4

First number: 3
Second number: 7
3 / 7 =  0.428571428571429
Select the operation: 5

Conclusion#

You learned how to generate menus using the select construct. It useful when require user input in shell scripts.

If you have any questions or feedback, feel free to leave a comment.

Related

Tags: bash, terminal

How to Solve “Sub-process /usr/bin/dpkg returned an error code (1)” In Ubuntu

Prev Post

How to Configure Static IP Address on Ubuntu 20.04

Next Post
Archives
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • July 2022
  • June 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
Categories
  • AlmaLinux
  • Android
  • Ansible
  • Apache
  • Arch Linux
  • AWS
  • Backups
  • Bash Shell
  • Bodhi Linux
  • CentOS
  • CentOS Stream
  • Chef
  • Cloud Software
  • CMS
  • Commandline Tools
  • Control Panels
  • CouchDB
  • Data Recovery Tools
  • Databases
  • Debian
  • Deepin Linux
  • Desktops
  • Development Tools
  • Docker
  • Download Managers
  • Drupal
  • Editors
  • Elementary OS
  • Encryption Tools
  • Fedora
  • Firewalls
  • FreeBSD
  • FTP
  • GIMP
  • Git
  • Hadoop
  • HAProxy
  • Java
  • Jenkins
  • Joomla
  • Kali Linux
  • KDE
  • Kubernetes
  • KVM
  • Laravel
  • Let's Encrypt
  • LFCA
  • Linux Certifications
  • Linux Commands
  • Linux Desktop
  • Linux Distros
  • Linux IDE
  • Linux Mint
  • Linux Talks
  • Lubuntu
  • LXC
  • Mail Server
  • Manjaro
  • MariaDB
  • MongoDB
  • Monitoring Tools
  • MySQL
  • Network
  • Networking Commands
  • NFS
  • Nginx
  • Nodejs
  • NTP
  • Open Source
  • OpenSUSE
  • Oracle Linux
  • Package Managers
  • Pentoo
  • PHP
  • Podman
  • Postfix Mail Server
  • PostgreSQL
  • Python
  • Questions
  • RedHat
  • Redis Server
  • Rocky Linux
  • Security
  • Shell Scripting
  • SQLite
  • SSH
  • Storage
  • Suse
  • Terminals
  • Text Editors
  • Top Tools
  • Torrent Clients
  • Tutorial
  • Ubuntu
  • Udemy Courses
  • Uncategorized
  • VirtualBox
  • Virtualization
  • VMware
  • VPN
  • VSCode Editor
  • Web Browsers
  • Web Design
  • Web Hosting
  • Web Servers
  • Webmin
  • Windows
  • Windows Subsystem
  • WordPress
  • Zabbix
  • Zentyal
  • Zorin OS
Visits
  • 0
  • 259
  • 614,631

DesignLinux.com © All rights reserved

Go to mobile version