Loading...
Shell Script

Read Command

To get the input from the keyboard, the read command is used. It takes the input the input and assigns it to a variable.The read command is of two types (i) read -p (ii) read -s

Syntax: read <Variable Name>

Example: read a

In the above example it reads the input and stores in the variable named “a”.

i) read  -p Option:

read -p option is used to print the output while reading a variable. This helps to reduce one echo command to display output. To use this command -p should be mentioned after read.

Syntax:  read -p  “Message to  be printed” variable

Example:  read -p “Enter the Name” name

ii) read -s Option:

read -s option is used silent read i.e., not to show the text on the screen that is entered.  This command is usually used to read the passwords. To use this command -s should be mentioned after read.

Syntax: read -p -s “Message to be displayed ” variable

Example: read -p -s “Enter the password” pass

The below example narrates the usage of Read Command:

#!/bin/bash
echo -n “Enter Name:”
read name
echo “The given name is : $name”
read -p ‘Enter Name: ‘ name
echo “The given name is : $name”
read -s -p ‘Enter Password : ‘ pass
echo “Password = $pass”

Leave a Reply

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