2

Given such situation:

echo "Please enter your name"
read name    # user enters: john smith
echo $name   # prints: john

What could cause read to read only the first word of the input? Is there a shell variable that controls this?

I came across this in a question on Ask Ubuntu and I'm wondering how to reproduce this behavior.

  • It has to have something to do with multiple fields - $IFS or filename generation. But echo concats on spaces - and so even for multiple args they should come together. I can imagine if it was an extra quote level deep - like an ssh script or something it might lose some - but that's fuzzy. echo should at least print the args unless $IFS is composed of all of the alphabetics or something. printf, on the other hand, would only print john or 123 in that scenario, though. – mikeserv Dec 27 '14 at 23:17
1

to accept whatever the user enters, use this form

IFS= read -r name

That will accept leading/trailing/inner spaces as well as literal backslashes.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.