7 Linux read command examples for Shell scripting

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
read VARIABLE

Example1: Read a value from user input.

	read VAR1

To display this value we have to use echo command.

	echo $VAR1

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

	read VAR1
	ARR1=($VAR1)

to display first value in array use below command

	echo ${ARR1[0]}

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.
Show your love by sharing this..!
Share on VKShare on StumbleUponShare on YummlyPrint this pageFlattr the authorShare on TumblrEmail this to someoneBuffer this pageShare on Google+Share on LinkedInShare on FacebookTweet about this on TwitterPin on PinterestShare on RedditDigg this
The following two tabs change content below.
My name is Surendra Kumar Anne. I hail from Vijayawada which is capital of south Indian state of Andhra Pradesh. I am a Linux evangelist who believes in Hard work, A down to earth person, Likes to share knowledge with others, Loves dogs, Likes photography. I work with ICE systems, Sydney as Sr. Consultant. You can contact me at surendra (@) linuxnix dot com.
  • Joe Chestnutt

    I have always found it useful to have an extra variable (I usually use JUNK) to catch any extra input.
    eg
    read VAR1 VAR2 VAR3 JUNK
    if [[ -n $JUNK ]];then
    echo Error: expecting 3 values
    exit 1
    fi

    • http://www.linuxnix.com Surendra

      good Joe,

      Appreciate your example. keep visiting linuxnix.com

  • thiru

    Hi Surender,

    i am getting error on this syntax, can you help me

    #!/bin/bash

    read name slNo designation << ( echo Madhu 78987 Planning )

    echo "name : $namenslNo : $slNOndesignation : $designation"

    OP:
    ./readFromCmdOp.sh: line 3: syntax error near unexpected token `('
    ./readFromCmdOp.sh: line 3: `read name slNo designation << ( echo Madhu 78987 Planning )'

  • thiru

    Hi Surender,

    i am getting error on this syntax, can you help me

    #!/bin/bash

    read name slNo designation << ( echo Madhu 78987 Planning )

    echo "name : $namenslNo : $slNOndesignation : $designation"

    OP:
    ./readFromCmdOp.sh: line 3: syntax error near unexpected token `('
    ./readFromCmdOp.sh: line 3: `read name slNo designation << ( echo Madhu 78987 Planning )'

  • http://pechalbata.com pechalbata.com

    Thanks to Joe Chestnutt

  • Vivekraj

    In Example 4 , it should be as follows

    ARR1=($VAR1) and not as ARR1=(VAR1)

    I am using Bash…!!! Thanks.

     

    • http://www.linuxnix.com Surendra Anne

      Yes my bad, Updated the post.

banner