Search

Limiting the number of characters read

read is used to take input from the user and assign it to a variable. read by default continues to accept input as long as the user does not enter the return key.

But if we want the input to be limited to a specific number of characters we can d it by using the option "-n" with read. i.e.



where num is the number of characters that is to accepted as input. In this case the read will not wait untill user hits return, but exits as soon as num characters are entered.

For example :

The option "-n" will exit even if it encounters a return key before the num characters are entered.

To avoid this,and to make sure that user must enter num characters before exiting read we can use the option "-N" i.e.



with the option "-N" the return key is also treated as in input character and read exits only after num characters are entered.




Follow by Email