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: )
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
What percentage of programmers have learned to touch type? Have you? Do you think it's important, not just for programming, but for more-than-casual computer users generally? ChatGPT didn't have much on it ("Research indicates that less than 20% of people can touch type fluently, with many relying on the hunt-and-peck method for typing ."). 'Hunt-and-peck method' made me smile. It added, "For programmers, touch typing is a valuable skill that can enhance speed, accuracy, and focus. While...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...
Back
Top