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

Re: If the variable has its 'integer' attribute set,



On Sun, Nov 21, 2021 at 07:18:09PM +0000, Darac Marjal wrote:
> You probably already know that if you write:
> 
> i = 65
> 
> then several things happen:

Only one thing happens: bash tries to run the command named "i" and
you will almost certainly get an error message saying "command not found".

If you want a variable assignment, you must not have spaces around the =
sign.

i=65

This assigns the string "65" to the variable named i.

> So, that's a very basic method of storing the variable.  At this point,
> we know that the variable "i" equals 65, right? Well, how do we know
> that the variable doesn't hold the character "A", or that it's not an
> array? So, there must be a table somewhere else that tells us "what KIND
> of data is stored in 'i'?".

Bash does not store integer variables.  Ever.  Only string variables.

Using "declare -i" does not change that.  It only makes bash undergo
some weird gyrations every time a new string value is assigned to the
variable in question.  (And overloads the += operator, because life wasn't
confusing enough yet.)

Many people think "Oh, if I use declare -i, bash will store my values
as integers, and arithmetic will become more efficient, because bash won't
have to keep doing string-to-integer conversions all the time."  This is
incorrect.  Everything is stored as a string.


Reply to: