Understanding the NAME_REGEX in Linux (used for checking usernames)

  • Thread starter Thread starter Wrichik Basu
  • Start date Start date
  • Tags Tags
    Linux
Click For Summary
SUMMARY

The discussion centers on the NAME_REGEX used for validating usernames in Linux, specifically within the Ubuntu app on Windows 10. The regex pattern is defined as ^[a-z][-a-z0-9_]*\$ and requires usernames to start with a lowercase letter, followed by zero or more characters from a specified set, concluding with a literal dollar sign. The confusion arises from the interpretation of the escaped dollar sign, which may not be necessary in certain contexts, as demonstrated by the user's experience with the username "wrichik-basu". The correct understanding of regex escaping is crucial for proper username validation.

PREREQUISITES
  • Understanding of regular expressions (regex)
  • Familiarity with Linux file structures, specifically /etc/adduser.conf
  • Basic knowledge of Bash programming
  • Experience with Ubuntu on Windows 10
NEXT STEPS
  • Study the Linux regex syntax in detail
  • Learn about the implications of escaping characters in regex
  • Explore username validation techniques in Bash scripting
  • Investigate the configuration options available in /etc/adduser.conf
USEFUL FOR

Linux system administrators, Bash programmers, and developers configuring user accounts in Ubuntu environments will benefit from this discussion.

Wrichik Basu
Science Advisor
Insights Author
Gold Member
Messages
2,180
Reaction score
2,690
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   Reactions: 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: )
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
2K
Replies
16
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 75 ·
3
Replies
75
Views
6K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 54 ·
2
Replies
54
Views
8K
  • · Replies 152 ·
6
Replies
152
Views
11K
  • · Replies 5 ·
Replies
5
Views
2K