read


Read from a line from standard input or file descriptor.

Syntax

read [OPTIONS] [NAME...]

If we want to read and store an input from a user, we can use the command read*.

#!/bin/bash
read variable1 variable2 variable3
echo The first value introduced is: $variable1
echo The second value introduced is: $variable2
echo The thrid value introduced is: $variable3
user@host:~$ ./script.sh value1 value2 value3
The first value introduced is: value1
The second value introduced is: value2
The thrid value introduced is: value3

But be careful when a user introduces more than the expected number of variables:

user@host:~$ ./script.sh value1 value2 value3 value4 value5
The first value introduced is: value1
The second value introduced is: value2
The thrid value introduced is: value3 value4 value5

Because the last variable will store the exceeding inputs.