2

I am using GNU bash - version 4.2.10(1). I want to read multiple variables using single read command in shell script. So i tried as below:

echo " Enter P N R : " read P N R 

but it's not working. It just ask for single value of P variable and returns prompt. I googled it but don't found any solution.

    1 Answer 1

    3

    read, without -r expects words on input to be delimited by the characters of the $IFS special parameter (by default SPC, TAB and NL, though as read reads only one line unless it ends in backslash, NL can't count) where backslash can be used to escape the separator or allow a line to be continued on the next physical line (backslash-newline sequences removed).

    So, here the user must enter the values for P, N, R space or tab separated, like:

    value_for_P value_for_N value_for_R 

    Or if the values can contain space:

    value\ for\ P value\ for\ N value for R 

    (here we didn't bother escaping the spaces for R as the rest of the line after the third word would end-up in R anyway; the user would still need to escape a trailing space though).

    If you want the user to enter the values on 3 lines, you'd need 3 read invocations. You'd then want -r to avoid the backslash processing and make IFS empty:

    IFS= read -r P IFS= read -r N IFS= read -r R 

      You must log in to answer this question.

      Start asking to get answers

      Find the answer to your question by asking.

      Ask question

      Explore related questions

      See similar questions with these tags.