Bash Script Read -P

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

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

Robert Thorne
Author

Robert Thorne

Robert Thorne covers electric vehicle innovations, autonomous driving systems, global mobility trends, and automotive engineering developments.