How to Get User Input in Linux Shell Script

Yahya Irmak Feb 02, 2024
  1. Use read to Get Input in Linux
  2. Use select to Give Options in Linux
  3. Use whiptail to Get Input via GUI in Linux
How to Get User Input in Linux Shell Script

This article will explain how to get user input in Linux Shell Script and give some examples.

  • read: to receive input from the user
  • select: to provide options
  • whiptail: to use the user interface

Use read to Get Input in Linux

The read command takes an input from the user and stores it in a variable.

read -p "Do you want to continue? (Y/y for Yes, any other key for No) " answer
    case $answer in
        [Yy]* ) echo "Program continues..."; break;;
        * ) echo "Program exits."; exit;;
    esac

Output:

Use read to Get Input in Linux

Use select to Give Options in Linux

The select command gives options to the user and stores the answer in a variable.

echo "Do you want to continue? "
select answer in "Yes" "No"; do
    case $answer in
        Yes ) echo "Program continues..."; break;;
        No ) echo "Program exits."; exit;;
    esac
done

Select

Use whiptail to Get Input via GUI in Linux

The whiptail command provides Yes/No options in the visual interface.

dialog, gdialog, or kdialog can be used depending on the system.

if whiptail --yesno "Do you want to continue? " 10 40 ;then
    echo "Program continues...";
else
    echo "Program exits."
fi

Whiptail

Author: Yahya Irmak
Yahya Irmak avatar Yahya Irmak avatar

Yahya Irmak has experience in full stack technologies such as Java, Spring Boot, JavaScript, CSS, HTML.

LinkedIn