The read command is useful in scripts when reading or asking an input from user. This read command is used when the script want to interact with user for his inputs to contunue the script.
read command syntax
Example1: Read a value from user input.
To display this value we have to use echo command.
Example2: Reading two words/variable/values at a time.
read VAR1 VAR2
Example3: Reading multiple values at a time.
read VAR1 VAR2 VAR3 VAR4
Example4: Read values in to an Array
to display first value in array use below command
Example5: Read values from a command
read VAR1 VAR2 VAR3 << ( echo surendra kumar anne )
echo "Enter values are $VAR1 $VAR2 $VAR3"
Example6: Read user input and give some info to user what he has to give. For this use -p option to display some info when reading value.
read -p "Please enter one to ten numbers: " VAL1
Note: You no need to echo command to display information to user, you can achieve that one using read -p option.
Example7: Read have inbuilt variable called REPLY. This is system variable which stores read value in to $REPLY.
read -p "Please enter a value"
echo "Enter value is $REPLY"
Please share your thoughts on this. We will see other scripting related to postings at http://scripting.linuxnix.com.
The following two tabs change content below.
Mr Surendra Anne is from Vijayawada, Andhra Pradesh, India. He is a Linux/Open source supporter who believes in Hard work, A down to earth person, Likes to share knowledge with others, Loves dogs, Likes photography. He works as Devops Engineer with Taggle systems, an IOT automatic water metering company, Sydney . You can contact him at surendra (@) linuxnix dot com.