LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-09-2020, 12:22 PM   #1
doru
Member
 
Registered: Sep 2008
Distribution: Ubuntu 8.04 LTS Server
Posts: 138

Rep: Reputation: 19
bash array first element += possible bug


Is this a bug? "+=" works like "=" for the first element of an array when it is not indexed and when some other conditions are met.
Code:
~/tmp]$ unset c
~/tmp]$ c=a
~/tmp]$ declare -i c
~/tmp]$ c[3]=10
~/tmp]$ c+=2
~/tmp]$ declare -p c
declare -ai c='([0]="2" [3]="10")'
~/tmp]$ c+=3
~/tmp]$ declare -p c
declare -ai c='([0]="3" [3]="10")'
~/tmp]$ c[0]+=3
~/tmp]$ declare -p c
declare -ai c='([0]="6" [3]="10")'

~/tmp]$ bash --version
bash --version
GNU bash, version 4.2.46(2)-release (x86_64-redhat-linux-gnu)
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
 
Old 01-09-2020, 12:47 PM   #2
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Here is the output I get when I run those commands:
Code:
$ unset c
$ c=a
$ declare -i c
$ c[3]=10
$ c+=2
$ declare -p c
declare -ai c='([0]="2" [3]="10")'
$ c+=3
$ declare -p c
declare -ai c='([0]="5" [3]="10")'
$ c[0]+=3
$ declare -p c
declare -ai c='([0]="8" [3]="10")'

$ bash --version
GNU bash, version 4.3.48(1)-release (x86_64-slackware-linux-gnu)
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
 
1 members found this post helpful.
Old 01-09-2020, 01:07 PM   #3
doru
Member
 
Registered: Sep 2008
Distribution: Ubuntu 8.04 LTS Server
Posts: 138

Original Poster
Rep: Reputation: 19
Thank you, good to know.
 
Old 01-13-2020, 12:52 PM   #4
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,789

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
Indeed this unlogical (buggy?) behavior exists in older bash versions; I verified with bash-3.2 and bash-4.1.
 
1 members found this post helpful.
Old 01-14-2020, 03:52 PM   #5
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (SW Chicago 'burbs)
Distribution: openSUSE, Raspbian, Slackware. Previous: MacOS, Red Hat, Coherent, Consensys SVR4.2, Tru64, Solaris
Posts: 2,800

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Quote:
Originally Posted by doru View Post
Is this a bug? "+=" works like "=" for the first element of an array when it is not indexed and when some other conditions are met.
[CODE]~/tmp]$ unset c
~/tmp]$ c=a
~/tmp]$ declare -i c
~/tmp]$ c[3]=10
~/tmp]$ c+=2
~/tmp]$ declare -p c
declare -ai c='([0]="2" [3]="10")'
So the moral of this story is that 'c' is equivalent to 'c[0]'?

I wonder if this behavior is actually mentioned in the nearly 6000 lines of the bash manpage.

Personally, I'd always include the index when referring to the array; I hate having to remember quirks like this.
 
Old 01-14-2020, 05:18 PM   #6
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,599

Rep: Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546
Quote:
Originally Posted by rnturn View Post
I wonder if this behavior is actually mentioned in the nearly 6000 lines of the bash manpage.
There might be 6000 lines of manpage, but the Array section is not even 6000 bytes, so it was fairly easy to find this sentence:
Quote:
Referencing an array variable without a subscript is equivalent to referencing with a subscript of 0.
Which makes it an officially documented feature (and clarifies that it's not limited to += operator).

Wouldn't surprise me if it was accidental behaviour that later got documented, rather than something deliberately implemented.
 
Old 01-14-2020, 08:33 PM   #7
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (SW Chicago 'burbs)
Distribution: openSUSE, Raspbian, Slackware. Previous: MacOS, Red Hat, Coherent, Consensys SVR4.2, Tru64, Solaris
Posts: 2,800

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Quote:
Originally Posted by boughtonp View Post
There might be 6000 lines of manpage, but the Array section is not even 6000 bytes, so it was fairly easy to find this sentence:

Which makes it an officially documented feature (and clarifies that it's not limited to += operator).
Well, references to arrays are scattered amongst about 5000 of those ~6000 lines. Most folks may not run across that one line---searching for it isn't trivial (it can depend on what size terminal window you're using, for example). But then manpages aren't really created as comprehensive reference manuals on Unix/Linux topics. The GNU tutorial on bash arrays is much more useful and I didn't see that quirk mentioned in there. (Doesn't mean it isn't in there; it's just not mentioned very prominently.) Still think it's a poor practice to follow documented or not. "[0]" just leaps out at ya and says "this is an array reference even though you may have forgotten about that 'declare' statement 200 lines above".

Slight Aside: Years ago, a co-worker discovered -- probably by accident (we were unable to find anything in Big Blues docs that said this was valid) -- that the IBM Fortran compiler would accept a statement like "DO100I=1,1000" instead of the "DO 100 I = 1, 1000" that would normally be used. He considered it a triumph of disk space savings ("Hey! Saved five bytes of disk space!") and went on to crank out a ton of code that had to be re-edited in case it was ever re-hosted on another vendor's system -- it eventually was, of course -- and thank $DIETY I had a student intern I could count on to help make those edits). Omitting "[0]" from bash arrays seems like the same pointless exercise in disk space or keystroke savings.

So what other interesting oddities are hiding in bash's syntax? :^)
 
Old 01-15-2020, 05:32 AM   #8
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Note: these two are both valid, but do different things:
Code:
DO100I=1.1000
DO100I=1,1000
 
Old 01-15-2020, 08:52 AM   #9
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,599

Rep: Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546
Quote:
Originally Posted by rnturn View Post
Well, references to arrays are scattered amongst about 5000 of those ~6000 lines.
Am I misreading or did you mistype something, because that seems to be saying that over 80% of the Bash manual refers to arrays?!

On an 80 column terminal I get 144 instances in 6175 lines (using man bash | wc -l and man bash | grep -io array | wc -l)


Quote:
Most folks may not run across that one line---searching for it isn't trivial (it can depend on what size terminal window you're using, for example).
Well the HTML manual has a table of contents, which means searching is <ctrl-f> "arrays" <click> rather than the "/arrays nnnnnnnn" it takes to get to the section in the man page, but the latter is not that bad, and even on a claustrophobic 80x24 terminal I'd still say it is trivial to find the reference.

The point you should be making is more that most people don't tend to go looking, at least not until they notice oddness, (and even then I find myself in a minority of people who actually check the documentation).

Languages should aim to follow the principle of least surprise just as much as other areas of computing, and this clearly violates that.


Quote:
Still think it's a poor practice to follow documented or not.
I'd condemn it with something stronger than "poor practice" - I only meant to suggest a bug report would probably be a waste of effort, not that it should ever be used.

Last edited by boughtonp; 01-15-2020 at 08:58 AM.
 
  


Reply

Tags
array, bash, bug



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] ksh unset on first element of associative array unsets the whole array luvshines Programming 4 05-29-2015 07:27 AM
BASH-Adding array element: Naming issue using array[${#array[*]}]=5 calvarado777 Programming 8 07-26-2013 09:48 PM
Does calling an element of an array calls the whole array unkn(0)wn Programming 3 07-06-2012 07:50 PM
[perl] copying an array element into another array s0l1dsnak3123 Programming 2 05-17-2008 01:47 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:38 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration