bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: eval, apparently inconsistent behavior


From: Chet Ramey
Subject: Re: eval, apparently inconsistent behavior
Date: Sun, 08 Jun 2008 22:32:50 -0400
User-agent: Thunderbird 2.0.0.14 (Macintosh/20080421)

antonio wrote:
#!/bin/bash

a=( 1 2 3 )
b=( 4 5 6 )

x=a
eval b=( \${$x[@]} )
echo ${b[@]}

#output:
#1 2 3


x=b
eval $x=( \${a[@]} )

#output:
#./tst: line 15: syntax error near unexpected token `('
#./tst: line 15: `eval $x=( \${a[@]} ) '

Strictly speaking, these are both syntax errors (unescaped metacharacters,
etc.).

Bash tries to detect words that appear to be assignment statements
in arguments to builtin commands that accept assignments (declare, eval,
local, export, and so on).  When it detects such an argument, it attempts
to treat it like an assignment statement preceding a simple command:
parsing compound assignments specially, no word splitting, no glob
expansion, etc.

You can fool this, especially in the case of `eval', as you discovered.
The argument to eval in the second assignment doesn't look like an
assignment statement to the parser, so it doesn't try to treat it like
one.

By the way, it's bad form to conceal your address and expect a response.

Chet
--
``The lyf so short, the craft so long to lerne.'' - Chaucer

Chet Ramey, ITS, CWRU    chet@case.edu    http://cnswww.cns.cwru.edu/~chet/




reply via email to

[Prev in Thread] Current Thread [Next in Thread]