Understanding the NAME_REGEX in Linux (used for checking usernames)

  • Thread starter Thread starter Wrichik Basu
  • Start date Start date
  • Tags Tags
    Linux
AI Thread Summary
The discussion centers on setting up the Ubuntu app on Windows 10 for Bash programming, specifically addressing an error encountered while setting a username due to regex constraints. The regex pattern for valid Linux usernames is identified as ^[a-z][-a-z0-9_]*\$ which requires usernames to start with a lowercase letter and can include hyphens, lowercase letters, digits, and underscores, with a literal dollar sign at the end. However, confusion arises regarding the necessity of the dollar sign, as examples of valid usernames like "wrichik-basu" do not include it. Further exploration reveals that the backslash used to escape the dollar sign may not be interpreted correctly in certain contexts, leading to the conclusion that the dollar sign is not required for usernames in practice. The clarification helps resolve the misunderstanding about the regex interpretation.
Wrichik Basu
Science Advisor
Insights Author
Gold Member
Messages
2,180
Reaction score
2,717
I was helping one of my classmates to set up the Ubuntu app on Windows 10 so that he can do Bash programming. While setting the username, he was getting an error due to bad regex, and that prompted me to look up the regex for username on Linux. This regex is stored in /etc/adduser.conf file. The regex is as follows: ^[a-z][-a-z0-9_]*\$

As far as I understand, this pattern wants the username to begin with a lower case letter. Then there can be zero or more characters, chosen from hyphen, lower case letters, digits, and underscore. At the end, the dollar sign is escaped, so there has to be a literal $ sign at the end.

regex101.com says that my interpretation is correct. In fact, user-name does not match the regex, but user-name$ matches.

However, my username is wrichik-basu, i.e., no $-sign at the end. While setting up the Ubuntu app for my classmate, no dollar sign was required either.

Am I wrong somewhere in understanding the regex?
 
Technology news on Phys.org
Wrichik Basu said:
At the end, the dollar sign is escaped, so there has to be a literal $ sign at the end.
The backslash might not actually end up in the regex, depending on how backslashes are interpreted in string literals.
Code:
$ echo "^[a-z][-a-z0-9_]*$"
^[a-z][-a-z0-9_]*$
$ echo "^[a-z][-a-z0-9_]*\$"
^[a-z][-a-z0-9_]*$
$ echo "^[a-z][-a-z0-9_]*\\$"
^[a-z][-a-z0-9_]*\$
$ echo '^[a-z][-a-z0-9_]*\$'
^[a-z][-a-z0-9_]*\$
 
  • Like
  • Love
Likes Wrichik Basu and Ibix
wle said:
The backslash might not actually end up in the regex, depending on how backslashes are interpreted in string literals.
Yes, you are right. Thanks, that explains it. (In fact, I am learning bash programming right now, and should have found that out myself. :doh: )
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
Thread 'Project Documentation'
Trying to package up a small bank account manager project that I have been tempering on for a while. One that is certainly worth something to me. Although I have created methods to whip up quick documents with all fields and properties. I would like something better to reference in order to express the mechanical functions. It is unclear to me about any standardized format for code documentation that exists. I have tried object orientated diagrams with shapes to try and express the...
Back
Top