Is there anyway to check for a valid username input in bash? For example, using the read command is there a way to ignore or interpret the left arrow input instead of storing ^[[D in the variable? What other input method can be used in bash other than read?
1 Answer
read -e will interpret arrows correctly. From man bash:
-eIf the standard input is coming from a terminal,readline(seeREADLINEabove) is used to obtain the line. Readline uses the current (or default, if line editing was not previously active) editing settings.
Checking whether an input string is a valid username is your responsibility, as it has nothing to do with inputting (e.g. see Check Whether a User Exists)
2 Comments
Friendly Fella
How can we check characters one by one to confirm that no
/ character is entered while using the read method?Amadan
"while using the
read method" - you can't. Again, nothing to do with inputting. You can check it later. See How to check if a string contains a substring in Bash.
readline. Most modernreadimplementations use thereadlinelibrary, which internally takes care of interpreting left arrow as cursor movement and not literal^[[D.readusesreadlineonly when it is passed the-eoption; as answered by Amadan.readlineyourself. Too much pain, if you ask me. Easier would be throwing an error after the user presses enter.