[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: syntax error near unexpected token `select'
From: |
Chet Ramey |
Subject: |
Re: syntax error near unexpected token `select' |
Date: |
Thu, 6 Dec 2001 16:35:29 -0500 |
> function whr()
> {
> local -a arg=( $(echo "$1"|cat) )
> echo arg=$arg
> }
>
> whr select
> bash: syntax error near unexpected token `select'
>
> whr test
> arg=test
>
> function whr2()
> {
> local -a arg
> arg=( $(echo "$1"|cat) )
> echo arg=$arg
> }
>
> whr2 select
> arg=select
You've managed to circumvent the parser in the second function.
`select' is a reserved word. Straight array assignments like
arg=(select)
will fail because when the compound assignment is parsed, the parser will
not return a `WORD' when `select' is parsed. (If you're curious, bash and
ksh will do the same thing here.) Parsing a compound array assignment
happens before its component words are expanded. That is,
nike.ins.cwru.edu(2)$ ./bash --version
GNU bash, version 2.05a.0(1)-release (i386-unknown-freebsd4.2)
Copyright 2001 Free Software Foundation, Inc.
nike.ins.cwru.edu(2)$ cat x12
a=( select )
echo a=${a[@]}
nike.ins.cwru.edu(2)$ ./bash ./x12
./x12: array assign: line 1: syntax error near unexpected token `select'
./x12: array assign: line 1: ` select '
a=
nike.ins.cwru.edu(2)$ ksh93 -c 'echo ${.sh.version}'
Version M 1993-12-28 m
nike.ins.cwru.edu(2)$ ksh93 ./x12
./x12: syntax error at line 1: `select' unexpected
The thing you have to remember about the first function is that local
(and its siblings typeset, declare, export, and readonly) is a shell
builtin. When it's invoked, the arguments have already been expanded
(with some trickery to avoid word splitting arguments that look like
variable assignments). `local', therefore, sees as its arguments `-a'
and `( select )'. As previously explained, that's a syntax error.
In the second function, when the compound assignment is parsed, the word
is not `select', but `$(echo "$1"|cat)'. That's parsed as a WORD, not
a reserved word. When it's expanded, it turns into `select', but that's
long after the parser has anything to do with it.
Chet
--
``The lyf so short, the craft so long to lerne.'' - Chaucer
( ``Discere est Dolere'' -- chet)
Chet Ramey, CWRU chet@po.CWRU.Edu http://cnswww.cns.cwru.edu/~chet/
- Re: syntax error near unexpected token `select',
Chet Ramey <=