Re: "printf %d ''" should diagnose the empty string
From:
Greg Wooledge
Subject:
Re: "printf %d ''" should diagnose the empty string
Date:
Mon, 10 Feb 2025 13:27:07 -0500
User-agent:
Mutt/1.10.1 (2018-07-13)
> On Mon, Feb 10, 2025 at 7:06 PM Andreas Schwab <schwab@suse.de> wrote:
> > Does it? If it did, it should have printed -23, not -19.
Oh, you're right.
On Mon, Feb 10, 2025 at 19:14:05 +0100, Phi Debian wrote:
> $ echo $BASH_VERSION
> 5.2.21(1)-release
> $ i=-0023 ; echo "$((i)) $((${i/?([-+])/&10#}))"
> -19 -23
hobbit:~$ cat x
var=-023
echo "$((var)) $((${var/?([-+])/&10#}))"
hobbit:~$ for v in 3.2 4.0 4.1 4.2 4.3 4.4 5.0 5.1 5.2; do printf '%s: ' "$v";
bash-"$v" x; done
3.2: -19 -19
4.0: -19 -19
4.1: -19 -19
4.2: -19 -19
4.3: -19 -19
4.4: -19 -19
5.0: -19 -19
5.1: -19 -19
5.2: -19 -19
hobbit:~$ cat y
shopt -s extglob
var=-023
echo "$((var)) $((${var/?([-+])/&10#}))"
hobbit:~$ for v in 3.2 4.0 4.1 4.2 4.3 4.4 5.0 5.1 5.2; do printf '%s: ' "$v";
bash-"$v" y; done
3.2: y: line 3: &10#023: syntax error: operand expected (error token is
"&10#023")
4.0: y: line 3: &10#023: syntax error: operand expected (error token is
"&10#023")
4.1: y: line 3: &10#023: syntax error: operand expected (error token is
"&10#023")
4.2: y: line 3: &10#023: syntax error: operand expected (error token is
"&10#023")
4.3: y: line 3: &10#023: syntax error: operand expected (error token is
"&10#023")
4.4: y: line 3: &10#023: syntax error: operand expected (error token is
"&10#023")
5.0: y: line 3: &10#023: syntax error: operand expected (error token is
"&10#023")
5.1: y: line 3: &10#023: syntax error: operand expected (error token is
"&10#023")
5.2: -19 -23
So, it silently fails in all bash versions if extglob is disabled (which
it is by default). And it noisily fails in all bash versions older
than 5.2 (where the & feature was added) if extglob is enabled.