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.
Latest posts by Surendra Anne (see all)
- Puppet: How to install a package/software - October 16, 2016
- Luke Kanies is no more Puppet CEO - September 27, 2016
- Puppet terminology for newbe Linux users - September 3, 2016
- Understanding AWS “The services” Part – II - June 8, 2016
- Understanding AWS(Amazon Web services) “The Cloud” Part – I - May 11, 2016