Skip to content

[BUG] $(( )) reads a variable with blanks around its number as 0 (n=$(wc -l < f); echo $((n+1)) prints 1) #368

Open

Description

@strandly-the-agent

Checks

  • I have updated to the latest minor and patch version of Strands Shell (built from main @ dbb6189)
  • I have checked the documentation and this is not expected behavior
  • I have searched ./issues and there are no duplicates of my issue

Binding

Rust

Strands Shell Version

dbb6189 (--version prints 0.0.0)

Language Runtime Version

Rust 1.100.0

Operating System

Linux 6.18 (aarch64)

Installation Method

built from source

Steps to Reproduce

strands-shell -c 'n=" 5"; echo $((n+1)); n="5 "; echo $((n+1))'
strands-shell -c 'printf "a\nb\n" > f; n=$(wc -l < f); echo "[$n]"; echo $((n+1))'

Expected Behavior

Same as bash:

6
6
[2]
3

Actual Behavior

1
1
[      2]
1

Nothing is printed on stderr and the status is 0.

Additional Context

Two things combine in the second command:

  • Arithmetic expansion reads a variable whose value has a space before or after the number as 0, without an error. Bash accepts the blanks.
  • wc pads a single count read from stdin to 7 columns. GNU wc prints 2.

Counting lines with wc -l and then doing arithmetic on the result is a common idiom, and here it gives a wrong number silently. #348 makes [/test accept blanks around an integer operand, so [ "$(wc -l < f)" -eq 2 ] becomes true there; $(( )) is a separate code path.

Found while reviewing #348; it is not caused by that PR. The results are the same on main @ dbb6189 and on #348's head 9ef3e56. I ran debug builds through -c only and compared with bash 5.2.15.

Possible Solution

Not investigated.

Related Issues

#348, #334

Activity

  1. added
    bug
    on Nov 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    Fields

    Language

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions