[Top][All Lists]

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

Re: Variables declared in arithmetic expressions have no i flag

From: Greg Wooledge
Subject: Re: Variables declared in arithmetic expressions have no i flag
Date: Tue, 24 Nov 2020 08:39:37 -0500
User-agent: Mutt/1.10.1 (2018-07-13)

On Tue, Nov 24, 2020 at 02:30:36PM +0100, Léa Gris wrote:
> Should variables automatically created within arithmetic constructs have the
> integer flag implied?

No.  Please, no.  It's bad enough that the -i flag exists in the first
place, without it being randomly added to poor innocent variables.

"Why did my code fail?"

"Bash 5.69 added a feature that puts -i on your variables sometimes, if
your first assignment to one just happens to occur inside an arithmetic
context.  And look here, you reused 'i' as both a numeric counter and
a for loop iterator, but it just so happens the numeric counter usage
occurred first, so you got slapped with the -i flag, and then your
for loop iterator blew up."

"OK, then why did this other script fail?"

"You assumed that 'n' would get the integer flag automatically since
you first assigned to it in a math command.  But someone exported it
as an environment variable before running your script, so it was already
a string variable at the time.  So you didn't get the -i flag on it."


reply via email to

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