Repository navigation
Description
Summary
awk fails to parse any expression where a numeric literal or field reference is
immediately followed by + or - with no space before the operator:
$ awk 'BEGIN{print 1+2}'
awk: invalid number: 1+2 # exit 1, expected: 3
Inserting a space before the operator makes it work, which points at the number
lexer rather than expression evaluation:
| expression | bashkit 0.17.1 | expected |
|---|---|---|
awk 'BEGIN{print 1 + 2}' |
3 |
3 |
awk 'BEGIN{print 1 +2}' |
3 |
3 |
awk 'BEGIN{print 1+ 2}' |
awk: invalid number: 1+ |
3 |
awk 'BEGIN{print 1+2}' |
awk: invalid number: 1+2 |
3 |
awk 'BEGIN{print 3-1}' |
awk: invalid number: 3-1 |
2 |
awk 'BEGIN{print 1e5+2}' |
awk: invalid number: 1e5+2 |
100002 |
echo '1 2' | awk '{print $1+$2}' |
awk: invalid number: 1+ |
3 |
echo '1 2' | awk '{print $1 + $2}' |
3 |
3 |
Only + and - are affected. *, /, %, ^ parse fine without spaces, and
identifier operands are unaffected:
| expression | result |
|---|---|
awk 'BEGIN{print 3*4}' |
12 ✅ |
awk 'BEGIN{print 10/4}' |
2.5 ✅ |
awk 'BEGIN{print 1%2}' |
1 ✅ |
awk 'BEGIN{print 2^3}' |
8 ✅ |
awk 'BEGIN{a=1;b=2;print a+b}' |
3 ✅ |
awk 'BEGIN{s=1; s+=2; print s}' |
3 ✅ |
Likely cause
+ and - are exactly the two characters that may legally follow e/E in a
numeric literal. awk 'BEGIN{print 1e+5}' and awk 'BEGIN{print 1.5e-3}' both
work, so the lexer does handle exponents — but it appears to keep consuming
+/- (and whatever follows) into the number token without requiring a
preceding e/E, and without stopping once a complete exponent has been read
(hence 1e5+2 also failing). The token is then handed to a numeric parse that
rejects it.
Reproduction
import asyncio, inspect
from bashkit import Bash
async def main():
for cmd in ["awk 'BEGIN{print 1+2}'", "awk 'BEGIN{print 1 + 2}'"]:
r = Bash().execute(cmd)
if inspect.isawaitable(r):
r = await r
print(cmd, "->", r.exit_code, repr(r.stdout), repr(r.stderr))
asyncio.run(main())awk 'BEGIN{print 1+2}' -> 1 '' 'execution error: awk: invalid number: 1+2\n'
awk 'BEGIN{print 1 + 2}' -> 0 '3\n' ''
Why this one hurts
awk 'BEGIN{print a+b}' and awk '{s+=$1} END{print s}' are the two idioms an
LLM reaches for when it needs arithmetic in a shell with no Python, and the
unspaced form is by far the more common way models write it. Because the failure
is a hard error rather than a wrong value, an agent typically responds by
rewriting the command several times before abandoning awk entirely — we saw a
production agent burn three to four extra tool calls per report on exactly this
before falling back to bc.
Environment
- bashkit 0.17.1 (latest on PyPI at time of writing), Python 3.12, macOS arm64
- Reproduced with a bare
Bash()— no custom builtins or non-default options
Activity