Ticket #36002

no whitespace trimming in arithmetic expansion

Open Date: 2016-01-30 21:54 Last Update: 2016-03-14 22:42

Reporter:
Owner:
Type:
Status:
Closed
Component:
MileStone:
(None)
Priority:
5 - Medium
Severity:
5 - Medium
Resolution:
Invalid
File:
None
Vote
Score: 0
No votes
0.0% (0/0)
0.0% (0/0)

Details

Unlike other shells, yash does not trim whitespace from variables in arithmetic expansion.

Consider:

var='  2  '
echo "|$((var))|"

ash, dash, ksh93, mksh, zsh, bash all output:

|2|

yash (no POSIX mode) outputs:

|  2  |

yash (POSIX mode) gives an error:

yash: arithmetic: `  2  ' is not a valid number

Workaround: $(($var)) (with the extra $) works as expected.


*Edit*: I found a way to trigger the error even in yash non-POSIX mode:

varname=num
num=' 1 '
: $((${varname} < 2))
echo end

This script produces an 'invalid number' error on yash (POSIX and non-POSIX) and outputs 'end' on all other shells.

Ticket History (3/6 Histories)

2016-01-30 21:54 Updated by: mcdutchie
  • New Ticket "no whitespace trimming in arithmetic expansion" created
2016-01-30 22:19 Updated by: mcdutchie
  • Details Updated
2016-01-30 22:41 Updated by: magicant
Comment

I'm wondering if this is indeed a bug. Does POSIX require the shell to trim such spaces?

2016-02-01 00:47 Updated by: mcdutchie
Comment

In fact I can't find anything in POSIX that either requires or prohibits this. It just seemed like every other shell does this and yash doesn't. But in the meantime I found one other shell that acts exactly like yash does: FreeBSD /bin/sh. So I guess this is technically not a bug. Perhaps it's still a real-world compatibility issue, though. POSIX seems to be silent on the issue, so you may want to consider whether following the behaviour of the majority of shells is preferable.

I must also correct myself on something else: yash (and FreeBSD /bin/sh) in fact trim leading whitespace, just not trailing whitespace. So the following succeeds on all shells I know of:

num='  1'
: $((num))
echo end
whereas the following fails on yash and FreeBSD /bin/sh:
num='1  '
: $((num))
echo end

2016-02-02 22:56 Updated by: magicant
Comment

Some shells including ksh and zsh seem to evaluate variables recursively in arithmetic expansion:

$ zsh -c 'v=1+1; echo $((v))'
2

Naturally, those shells ignore surrounding whitespaces in variables:

$ zsh -c 'v=" 1 "; echo $((v))'
1

Dash does not evaluate variables recursively, but still ignores whitespaces. I don't know why dash does so.

Yash uses the wcstol function to parse the value in variables. The function ignores leading whitespaces, but not trailing ones. Yash's behavior is natural consequence of the function's behavior. For simplicity of implementation, I'm not very inclined to change the current behavior.

2016-03-14 22:42 Updated by: magicant
  • Resolution Update from None to Invalid
  • Status Update from Open to Closed
  • Ticket Close date is changed to 2016-03-14 22:42

Attachment File List

No attachments

Edit

You are not logged in. I you are not logged in, your comment will be treated as an anonymous post. » Login