Interactive Shell scripts – reading user input
The read command is a shell built-in command for reading data from a file or keyboard.
The read command receives the input from the keyboard or a file until it receives a newline character. Then, it converts the newline character into a null character:
Read a value and store it in the variable, shown as follows:
read variable echo $variable
This will receive text from the keyboard. The received text will be stored in the variable.
Command read with prompt. Whenever we need to display the prompt with certain text, we use the
–poption. The option-pdisplays the text that is placed after–pon the screen:#!/bin/bash # following line will print "Enter value: " and then read data # The received text will be stored in variable value read -p "Enter value : " value
Output:
Enter value : abcdIf the variable name is not supplied next to the
readcommand, then the received data or text will be stored in a special built-in variable calledREPLY. Let's write...