Ubuntu – Bash Script read -p

bashscripts

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?

Best 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
Related Question