Use:
or
or
The quotes cause an empty $test to expand to a null string (""), which behaves as it should:
Without that, the empty $test expands to nothing at all, and the test becomes:
i.e. it tests if "]" is a non-null string! "[" has an odd syntax. Although it is built into bash, traditionally it's an external command (see "/usr/bin/["), so the "]" is an optional part of its command line...
"[[ ... ]]" is guaranteed to be a shell built-in, and because of that is aware of $test being a shell variable (rather than only getting the expanded version on its command line), so the quotes are optional.
The "[ ... ]" syntax is for compatibility with traditional Bourne shell, POSIX prefers "[[ ... ]]" for new code.
Gareth
---------- Post added at 12:06 AM ---------- Previous post was at 12:01 AM ----------
Also note that your script will catch
any match in /etc/passwd, not just user names. Try adding a user "bash" with your script for example.