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

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




On 21/11/2021 01:37, sim sim wrote:
Hi, all.
My question may be in the wrong place but I'm already exhausted. Started reading https://www.gnu.org/software/bash/manual/bash.pdf.

3.4 Shell Parameters
.........
....
A parameter is set if it has been assigned a value.  The null string is a valid value.  Once a variable is set, it may be unset only by using the 'unset' builtin command.
   A variable may be assigned to by a statement of the form
     NAME=[VALUE]
If VALUE is not given, the variable is assigned the null string.  All VALUEs undergo tilde expansion, parameter and variable expansion, command substitution, arithmetic expansion, and quote removal (detailed below).  If the variable has its 'integer' attribute set, then VALUE is evaluated as an arithmetic _expression_ even if the '$((...))' expansion is not used.  
.............
......
I do not understand  "If the variable has its 'integer' attribute set," where the variable has an 'integer' attribute, after all, this is not a function?
I do not ask to explain in detail, because it is long and I will not understand because of my poor English, but if you can just write a simple variable for:
 name=[value]
where "If the variable has its 'integer' attribute set",
for example it will help a lot. This manual is difficult for a beginner to read. The same concept in different places denotes: either an attribute or a parameter or an option.
And advise a smart forum on this topic, where you can ask questions, Google found trash.

The way I would explain it is to assume that bash stores variables with "data" and "metadata" separately.

You probably already know that if you write:

i = 65

then several things happen:

* a block of memory is allocated

* the address of the allocated memory is stored somewhere, and a reference to that address is associated with the variable "i"

* The value "65" is stored in that memory location

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'?".

From a naive point of view, we might use an enumeration for the type (if the type is 1 it's an integer, if it's 2 it's a string, if it's 3 it's an array and so on), but the authors of bash, for whatever reason, seem to use type flags. Perhaps a variable can be an integer *and* a string? I'm not sure.

In summary, though, I would read "If the variable has its 'integer' attribute set" as synonymous with "If the variable is declared an integer". The bit to be careful about is that the value is NOT evaluated as an arithmetic if the variable merely LOOKS like an integer (e.g. the string "65").


Thanks,
Sim.

Attachment: OpenPGP_signature
Description: OpenPGP digital signature


Reply to: