bug-bash
[Top][All Lists]
Advanced

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

Re: Compare 2 arrays.


From: Greg Wooledge
Subject: Re: Compare 2 arrays.
Date: Wed, 30 May 2012 12:57:50 -0400
User-agent: Mutt/1.4.2.3i

On Wed, May 30, 2012 at 10:14:42AM -0600, Bill Gradwohl wrote:
> What say you Chet? Bug or feature? There is no middle ground.

That's unrealistic.  There are plenty of things that occupy that middle
ground -- unexpected program behaviors.  The programmer can never
anticipate *every* input sequence that users will throw at the software,
so some of them may cause surprises.

The danger of using unexpected program behaviors in your applications
is that the behaviors may change without warning in a future version of
the program.  The programmer may not even be aware that this behavior
exists, let alone that people are using it.  A clean-up of the parser
(or similar change) may make the behavior go away, or change.

http://mywiki.wooledge.org/BashFAQ/006 has this to say on the matter
(right after demonstrating ksh93's nameref):

  We are not aware of any trick that can duplicate that functionality
  in POSIX or Bourne shells (short of using eval, which is extremely
  difficult to do securely). Bash can almost do it -- some indirect
  array tricks work, and others do not, and we do not know whether the
  syntax involved will remain stable in future releases. So, consider
  this a use at your own risk hack.

  # Bash -- trick #1.  Seems to work in bash 2 and up.
  realarray=(...) ref=realarray; index=2
  tmp="$ref[$index]"
  echo "${!tmp}"            # gives array element [2]

  # Bash -- trick #2.  Seems to work in bash 3 and up.
  # Does NOT work in bash 2.05b.
  tmp="address@hidden"
  printf "<%s> " "${!tmp}"; echo    # Iterate whole array.

I added that on 2011-05-02 after Buglouse described it on IRC.  I'm fairly
certain that EVERYONE else in the channel at the time was as surprised
by it as I was.



reply via email to

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