bug-bash
[Top][All Lists]
Advanced

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

Re: language inconsistency(wart) & RFE


From: Greg Wooledge
Subject: Re: language inconsistency(wart) & RFE
Date: Thu, 22 Oct 2015 08:25:44 -0400
User-agent: Mutt/1.4.2.3i

On Wed, Oct 21, 2015 at 07:19:30PM -0700, Linda Walsh wrote:
> Not to mention your statement doesn't work:

My code worked.  I tested it.  If you take working code and mangle it
according to your incredibly bizarre notions of how bash code should
look, and it doesn't work, then the problem is on your end.

> function nint {
>  local i j k
>  for ((i=0; i<10; ++i)) ; do j+=i; k+=j ;done

THAT.  IS.  NOT.  HOW.  YOU.  DO.  ARITHMETIC.

You want to perform integer addition?  Then you use the syntax that
specifies integer addition.

j+=i   is the syntax that specifies string concatenation.

To perform arithmetic, you use $((..)) or ((..)) or let.

nint() {
  local i j k
  for ((i=0; i<10; i++)); do ((j+=i, k+=j)); done
  declare -p i j k
}


imadev:~$ nint
declare -- i="10"
declare -- j="45"
declare -- k="165"


I have stated this before, but apparently it didn't sink it, so I'll
try again in all caps: DO NOT USE declare -i EVER.

If you use declare -i then you BREAK the ability to read your script and
see what a command is doing.  You pervert the string concatention syntax
into performing integer addition, but only some of the time, and you can't
tell which times that is.  (OO people call this "overloading" and think
that it's a feature rather than an abomination.)  (I do not and never
will understand OO people.  They must have been brainwashed.)

So now that you know not to use declare -i, all of your other problems
just go away!

You use local when you want local variables, and you don't use any
declaration at all when you want global (or dynamically scoped)
variables.

That provides the ability you have been asking for: to decide on a
case-by-case basis which variables will be local and which will not.

The end.



reply via email to

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