Skip to content

05. Reading User Input

Basic Syntax

read variable_name
  • Waits for user input.

  • Stores the entered value into a variable.

  • Input is read until the user presses Enter.


Example Script

read.sh

#!/bin/bash

echo -e "Hi, please type the word: \c "
read word
echo "The word you entered is: $word"

echo -e "Can you please enter two words? "
read word1 word2
echo "Here is your input: \"$word1\" \"$word2\""

echo -e "How do you feel about bash scripting? "
# 05. Reading User Input
read
echo "You said $REPLY, I'm glad to hear that! "

echo -e "What are your favorite colours ? "
# -a makes read command read input into an array
read -a colours
echo "My favorite colours are also ${colours[0]}, ${colours[1]} and ${colours[2]}:-)"

Example Execution

$ ./read.sh

Hi, please type the word: Linuxconfig.org
The word you entered is: Linuxconfig.org

Can you please enter two words?
Debian Linux
Here is your input: "Debian" "Linux"

How do you feel about bash scripting?
good
You said good, I'm glad to hear that!

What are your favorite colours ?
blue green black
My favorite colours are also blue, green and black:-)

Explanation

1. Reading Single Input

read word
  • Reads one line of user input.

  • Stores it in variable word.

Example:

Input  → Linuxconfig.org
Stored → $word

2. Reading Multiple Variables

read word1 word2
  • Splits input using spaces.

  • First word → word1

  • Second word → word2

Example:

Input: Debian Linux
word1 = Debian
word2 = Linux

If more words are entered, the remaining words go into the last variable.


3. Using Default Variable $REPLY

read

When no variable name is provided:

  • Bash automatically stores input in $REPLY.

Example:

Input → good
Stored → $REPLY

4. Reading Input into an Array (-a option)

read -a colours
  • Stores space-separated values into an array.

  • Each word becomes an element.

Example:

Input: blue green black

colours[0] = blue
colours[1] = green
colours[2] = black

Access elements using:

${colours[index]}

Understanding echo -e and \c

echo -e "Hi, please type the word: \c "
  • -e enables escape characters.

  • \c prevents a newline, keeping the cursor on the same line for input.

Without \c, the input would appear on the next line.


Common read Options

Option Description
-a Store input into an array
-p Display prompt message
-s Silent input (useful for passwords)
-t Timeout for input
-n Read limited number of characters

Example with prompt:

read -p "Enter your name: " name

Practical Example

Reading a password securely:

read -s -p "Enter password: " password
echo
echo "Password received"
  • -s hides typed characters.

Summary

  • read allows user interaction in Bash scripts.

  • Input can be stored in variables, multiple variables, or arrays.

  • $REPLY stores input when no variable is specified.

  • Options like -a, -p, and -s enhance input handling.

  • Essential for creating interactive Bash programs.

The read command is a fundamental tool for building dynamic and user-driven shell scripts.