H2ONaCl
H2ONaCl

Reputation: 11279

globbing behaviour is being replaced by the print of a token

This is a test of globbing. Note that globbing works as expected for $PATH and $NOTHER... but not for $JAVACLASSPATH. Instead the token is echoed even though there is no match because there are no file names that contain a colon :.

> ls -l
total 8
-rw-rw-r-- 1 brian brian   6 Sep  7 16:20 lib.txt
-rwxrwxr-x 1 brian brian 102 Sep  7 16:18 test.sh
> cat test.sh
#!/bin/bash
PATH='*'
JAVACLASSPATH='lib:*'
NOTHER='lib*'
echo $PATH
echo $JAVACLASSPATH
echo $NOTHER

> ./test.sh
lib.txt test.sh
lib:*
lib.txt
> 

Why is the token echoed? It seems that globbing is disabled which is surprising (but convenient for Java programmers because a glob expansion uses space delimiters which is wrong on Linux where java needs : delimiters between classes and it's also convenient because * has a special meaning for java because it means "all the JAR files").

Edit: As heemayl has shown, when there is no match glob produces an unchanged token and it is documented behaviour. This means a Java classpath will see the * which means "all the JAR files" which could be a nice side-effect but surprising side-effects are bad.

Upvotes: 1

Views: 60

Answers (1)

heemayl
heemayl

Reputation: 42017

That's because you are matching files in the current directory with the glob patterns and there is no file that starts with lib: in the current directory. If you had a file like lib:txt then the glob pattern lib:* would expand to that instead.

Also when any file is not matched by the glob pattern, the pattern is treated literally, quoting relevant portion of man bash:

If no matching filenames are found, and the shell option nullglob is not enabled, the word is left unchanged.

if you want you can prevent this behavior by using nullglob shell option:

shopt -s nullglob

Upvotes: 3

Related Questions