[email protected]Support 24/7 · Billing 09:00 – 00:00LinkedInXFacebook
NexonHost
Netherlands Dedicated ServersAmsterdam · Equinix AM6Germany Dedicated ServersFrankfurt · Equinix FR4Romania Dedicated ServersBucharest · VoxilityAustria Dedicated ServersVienna · Interxion VIEBulgaria Dedicated ServersSofia · TelepointFrance Dedicated ServersParis · Interxion PAR — Marseille · Digital Realty MRS3Ireland Dedicated ServersDublin · Equinix DB2Italy Dedicated ServersMilan · Irideos AvalonPoland Dedicated ServersWarsawSpain Dedicated ServersMadrid · Equinix MD2United States Dedicated ServersAshburn · Equinix DC
All articles
Getting startedSeptember 12, 2023

How to use slect in bash.

How To Use Slect In Bash.

In this tutorial, we will cover the basics of the select construct in Bash.

The select construct allows you to generate menus.

 

Bash select Construct

The select construct generates a menu from a list of items. It has almost the same syntax as the for loop:

select ITEM in [LIST]
do
  [COMMANDS]
done

The [LIST] can be a series of strings separated by spaces, a range of numbers, output of a command, an array, and so on. A custom prompt for the select construct can be set using the PS3 environment variable .

When the select construct is invoked, each item from the list is printed on the screen (standard error), preceded with a number.

If the user enters a number that corresponds to the number of one of the displayed items, then the value of [ITEM] is set to that item. The value of the selected item is stored in the variable REPLY. Otherwise, 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.

To demonstrate how the select construct works, let’s take a look at the following simple example:

PS3="Enter a number: "

select character in Sheldon Leonard Penny Howard Raj
do
    echo "Selected character: $character"
    echo "Selected number: $REPLY"
done

The script will display a menu consisting of list items with an accompanying number and the PS3 prompt. When the user enters a number, the script will print the selected character and number:

1) Sheldon
2) Leonard
3) Penny
4) Howard
5) Raj
Enter a number: 3
Selected character: Penny
Selected number: 3
Enter a number:

 

Bash select Example

Usually, select is used in combination with case of if statements.

Let’s take a look at a more practical example. It is a simple calculator that prompt the user for input and performs basic arithmetic operations like addition, subtraction, multiplication, and division.

PS3="Select the operation: "

select opt in add subtract multiply divide quit; do

  case $opt in
    add)
      read -p "Enter the first number: " n1
      read -p "Enter the second number: " n2
      echo "$n1 + $n2 = $(($n1+$n2))"
      ;;
    subtract)
      read -p "Enter the first number: " n1
      read -p "Enter the second number: " n2
      echo "$n1 - $n2 = $(($n1-$n2))"
      ;;
    multiply)
      read -p "Enter the first number: " n1
      read -p "Enter the second number: " n2
      echo "$n1 * $n2 = $(($n1*$n2))"
      ;;
    divide)
      read -p "Enter the first number: " n1
      read -p "Enter the second number: " n2
      echo "$n1 / $n2 = $(($n1/$n2))"
      ;;
    quit)
      break
      ;;
    *) 
      echo "Invalid option $REPLY"
      ;;
  esac
done

When the script is executed, it displays the menu and the PS3 prompt. The user is prompted to select the operation and then to enter two numbers. Depending on the user’s input, the scrip will print the result. The user will be asked to perform a new operation after each selection until the break command is executed.

1) add
2) subtract
3) multiply
4) divide
5) quit
Select the operation: 1
Enter the first number: 4
Enter the second number: 5
4 + 5 = 9
Select the operation: 2
Enter the first number: 4
Enter the second number: 5
4 - 5 = -1
Select the operation: 9
Invalid option 9
Select the operation: 5

One drawback of this script is that it can work only with integers.

Here is a little more advanced version. We are using the bc tool that supports floating numbers to perform mathematical calculations. Also, the repetitive code is grouped inside a function .

calculate () {
  read -p "Enter the first number: " n1
  read -p "Enter the second number: " n2
  echo "$n1 $1 $n2 = " $(bc -l <<< "$n1$1$n2")
}

PS3="Select the operation: "

select opt in add subtract multiply divide quit; do

  case $opt in
    add)
      calculate "+";;
    subtract)
      calculate "-";;
    multiply)
      calculate "*";;
    divide)
      calculate "/";;
    quit)
      break;;
    *) 
      echo "Invalid option $REPLY";;
  esac
done
1) add
2) subtract
3) multiply
4) divide
5) quit
Select the operation: 4
Enter the first number: 8
Enter the second number: 9
8 / 9 =  .88888888888888888888
Select the operation: 5   

 

Conclusion

The select construct allows you to easily generate menus. It is especially useful when writing shell scripts that require user input.

Keep reading
How to use iptables
February 21, 2023

How to use iptables

How to use iptables The user-space application program iptables allows configuring the tables provided by the Linux kernel firewall, as well as the chains and rules it stores. The kernel module currently used for iptables only applies to IPv4 traffic, to configure firewall rules for IPv6…

Read article
How to install java on CentOs.
February 3, 2023

How to install java on CentOs.

How To Install Java On CentOs. First update all packages. Use these command: 2. Install OpenJDK package with these comand: 3. Install OpenJDK-devel package: 4. Check if the package is installed properly through rpm tool: 5. Now check the java version: This is how to install Java in five steps.

Read article
How To Use grep Command In Linux
January 30, 2023

How To Use grep Command In Linux

How To Use Grep Command In Linux Grep is an essential Linux and Unix command. It is used to search text and strings in a given file. In other words, grep command searches the given file for lines containing a match to the given strings or words. The syntax is as follows: grep ‘word’ filename…

Read article
Get in Touch

Together, Let’s Build a Faster, Safer Internet.

Build scalable infrastructure with NexonHost — high-performance dedicated servers, VPS hosting, cloud hosting, and DDoS protection across Europe.

Get Started →Contact Us
[email protected]Support 24/7 · Billing 09:00 – 00:00