[* is a regular command, similar to
cat. You should be able to find it in
/bin. Since it's a separate program, the shell will perform its normal set of expansions before handing
[ its arguments.
As has been mentioned, since you're using
* in your tests, you're getting glob expansions. Note that even if you use quotes, such as
'hel*', this probably won't work as you hope, because
[ does not support patterns. In the case of
h*o working, that is likely due to the presence of a file named
hello in your current directory, and no other files matching that pattern. If it does work without a
hello file, you may have an odd implementation, and your script is likely to fail on other systems.
Depending on your needs, there are a couple of options. Bash, Zsh, and some other shells have the
[[ builtin. Since it is a builtin, it can give its arguments special treatment, including avoiding glob expansion. Additionally, it can do pattern matching. Try
if [[ "$var1" = hel* ]]; then
Also, note the lack of quotes around the pattern. Without quotes,
hel* is treated as a pattern by
[[, with quotes (single or double),
"hel*" is treated literally.
If you need wider compatibility, such as for shells without
[[, you can use grep:
if echo "$var1" | grep -qe 'hel.*' ; then
[[ necessary here, but the quotes around
*Some shells actually do have
[ builtin, but this is for efficiency purposes. It should still behave identically to the separate executable, including having its arguments subjected to the shell's normal "mangling."