Bash Script read -p

I'm pretty sure the answer is no but I thought I'd ask anyway. I want to have a bash script with a user input that automatically proceeds after the desired amount of characters are entered, in this case 3.

So when the user enters 123 or abc, the script moves on with that variable rather than waiting for an 'Enter'.

read -p 'User Input: ' userInput

Is this possible?

1 Answer

In bash, you can add -n 3 or -N 3 depending on the exact behavior you require. From help read:

 -n nchars return after reading NCHARS characters rather than waiting for a newline, but honor a delimiter if fewer than NCHARS characters are read before the delimiter -N nchars return only after reading exactly NCHARS characters, unless EOF is encountered or read times out, ignoring any delimiter
3

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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

You Might Also Like