Interactive shell scripts – reading user input

The read command is a built-in shell 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:

  1. 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.

  1. Whenever we need to display the prompt with certain text, we use the -p option. The option -p displays the text that is placed after -p on the screen:
#!/bin/bash # following line will print "Enter value: " and then read data # The received text will be stored ...

Get Learning Linux Shell Scripting - Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.