help-bash
[Top][All Lists]
Advanced

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

Re: Fwd: Undocumented behaviour of 'set -u' - errors when variable IS de


From: #!microsuxx
Subject: Re: Fwd: Undocumented behaviour of 'set -u' - errors when variable IS defined but non-number is used in numeric comparisons
Date: Fri, 15 Nov 2024 21:17:24 +0100

On Fri, Nov 15, 2024, 9:03 PM Greg Wooledge <greg@wooledge.org> wrote:

> On Fri, Nov 15, 2024 at 20:52:00 +0100, #!microsuxx wrote:
> > try declare -i num=not
>
> hobbit:~$ bash
> hobbit:~$ set -u
> hobbit:~$ declare -i num=not
> bash: not: unbound variable
>
> Again, this creates an arithmetic expansion context, so the string
> "not" is treated as an unset (unbound) variable and triggers the
> error when -u is in effect.
>

very good

The OP either needs to turn off -u, or stop evaluating variables that
> may contain words inside math contexts.  Perhaps what they really
> need is *input validation*.  If you receive a value and store it in
> a variable that's supposed to contain a number, you should verify that
> it actually is a number before you use it.
>

<https://mywiki.wooledge.org/BashFAQ/054> has tips for validating numbers.
>

id only include a [:[digit]:] version .. many dont have us ascii 0 - 9 as
num , n stuff

<https://mywiki.wooledge.org/CodeInjection#Arithmetic_Expansion>
> demonstrates why it's *vital* to validate inputs before using them
> in bash's arithmetic.
>
>


reply via email to

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