said...
>% In /bin/sh AT&T V4.0
>% How does the shell interpret [ ] square brackets?
>Hmmm... Not too sure, since I'm on a Sun at 4.1.1, but I think it
>doesn't. At all.
% man sh
SH(1) USER COMMANDS SH(1)
...
Filename Generation
...
[...]
Matches any one of the enclosed characters. A
pair of characters separated by `-' matches any
character lexically between the pair, inclusive.
If the first character following the opening [ is
a ! any character not enclosed is matched.
The shell does interpret brackets. They're part of the wildcard set.
This is true on any /bin/sh I know of (including our SunOS).
Quote:>% I find that if I do not quote brackets, I get unpredictable (to me (-:
>% results). For example, I found that:
>%
>% foobar | tr [a-b] [A-B]
The tr [a-b] [A-B] command will, if you're lucky, simply run tr with
the arguments [a-b] and [A-B] (in sh, wildcard patterns are left alone
if they don't match anything). If you're unlucky, there will be a file
called "a" or "B" or something in the current directory. In that case,
[a-b] or [A-B] will be replaced with the list of matched filenames, which
is not what you want.
Quote:>This seems as though it would run two tests (under SunOS, [ is the
>program usr/bin/[ and is linked to /usr/bin/test), thus giving you
>strange output.
The [ will only be interpreted as a command if it is in command position.
Also, under SunOS, [ and test are built into /bin/sh; they're not external
commands:
$ PATH=/
$ [
test: ] missing
Quote:>% does not produce the same output as
>% `foobar | tr [a-b] [A-B]`
>This should also run tests, though. Hmmm...
I'm not sure why this would produce different results. Probably, you
ran it in a different directory than the previous example. The other
directory didn't have the one-character filenames in it, thus producing
different results.
Quote:>% I have to quote the brackets to make it work like this:
>%
>% `foobar | tr '['a-b']' '['A-B']'
>This is the only one which will pass the square brackets to the tr
>command-- or at least, it should be.
The earlier ones will pass the square brackets to tr, but only if you're
lucky. Always quote the brackets just to be sure. Quote the whole
argument, in fact; it's easier:
foobar | tr '[a-b]' '[A-B]'
--
Paul Falstad | 10 PRINT "PRINCETON CS"