How to Prompt for User Input in Bash/Shell Scripts

Bash Prompt For Input

This short tutorial will teach you to prompt the user for typed input from your Bash/Shell scripts. It’s easy to learn, easy to do, so read on!

The read Command

To read user input in shell scripts, use the aptly named read command. It has the following syntax:

read OPTIONS VARIABLES

Note that:

  • The read command will read a line from standard input and split that input into fields
    • Usually, standard input is the terminal with input from your keyboard, but you can also pipe or redirect input to the read command
    • From the users perspective, they will be prompted and must enter something followed by pressing the ENTER key
  • OPTIONS should be a list of option flags from the below table which will modify the behavior of the read command
  • VARIABLES is a list of values that will be passed to the script separated by spaces
    • If the -p option is used, you can pair this with a matching list of prompts to tell the user what they should be entering
    • As multiple VARIABLES can be supplied separated by spaces, any value you wish to input, which includes spaces or other special characters, should be quoted
read Commonly Used Options
-a array Assign the words read to sequential indices of the array variable ARRAY, starting at zero
-p prompt Output the string PROMPT without a trailing newline before attempting to read
-r Do not allow backslashes to escape any characters
-s Do not echo input coming from a terminal
-t timeout Time out and return failure if a complete line of input is not read within TIMEOUT seconds

As usual, you can get the user manual for the read command, including a full list of available options, by running:

man read

Prompting for Input Using the Read Command

Here are some examples of using the read command to accept user input in Bash/Shell scripts.

But first…

What is the ‘#!’ in Linux Shell Scripts?

Just Getting Input

This is the simplest usage of the command – simply reading a single value from input:

#!/bin/bash

read word
echo You just typed $word

Above, input is read into a variable called $word, and then displayed using the echo command. Again, there will be no prompting – the screen will halt and wait for the user to type something, followed by the ENTER key – then the script will resume with the given input.

To get multiple input values, add more variables to the command, separated by spaces:

#!/bin/bash

read word1 word2
echo You just typed $word1 followed by $word2

Prompting for Input with a Message

Having the screen pause to wait for input isn’t really that user-friendly. Usually, you’ll want to tell the user what they are supposed to be entering – a username or a password, for instance. This next example does just that:

#!/bin/bash

read -p 'Username: ' username
read -sp 'Password: ' password
echo Your username is $username, we will not display your password

Above, the -p option is used to display a prompt – a message displayed telling the user what they should input for each variable’s value.

You can also see the -s option used for the password variable, which silences the typed input so that it is not displayed.

Prompting for Multiple Input With a Message

You can prompt and then read multiple values:

#!/bin/bash

read -p 'Please type your first and last name: ' first last
echo Your name is $first $last

Prompting for Yes/No

Here’s how to ask the user a simple yes/no question using a while loop and a case statement. Any string starting with y or Y will be interpreted as a yes*, any string starting with a n or a N will be interpreted as a no:

#!/bin/bash
while true; do
    read -p "Do you wish to perform this action?" yesno
    case $yesno in
        [Yy]* ) 
            echo "You answered yes"
        ;;
        [Nn]* ) 
            echo "You answered no, exiting"
            exit
        ;;
        * ) echo "Answer either yes or no!";;
    esac
done

The script will execute until a no is received – and can be easily modified to prompt the user to perform any action.

SHARE:
nv-author-image

Brad Morton

I'm Brad, and I'm nearing 20 years of experience with Linux. I've worked in just about every IT role there is before taking the leap into software development. Currently, I'm building desktop and web-based solutions with NodeJS and PHP hosted on Linux infrastructure. Visit my blog or find me on Twitter to see what I'm up to.

Leave a Reply

Your email address will not be published. Required fields are marked *