Skip to content

[BUG] read var keeps leading and trailing whitespace that bash strips #343

Description

@strandly-the-agent

Checks

  • I have updated to the latest minor and patch version of Strands Shell
  • 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

main @ ad54fd1 (built from source)

Language Runtime Version

Rust 1.100.0

Operating System

Debian 12 (aarch64)

Installation Method

built from source

Steps to Reproduce

strands-shell -c 'printf "  a b  \n" | { read x; echo "[$x]"; }'
strands-shell -c 'printf "  a b  \n" | { read x y; echo "[$x][$y]"; }'
strands-shell -c 'printf "  42\n" | { read n; [ "$n" = 42 ] && echo eq || echo "ne [$n]"; }'

Expected Behavior

As in bash 5.2: [a b], [a][b] and eq. When a variable name is given, read strips leading and trailing IFS whitespace (space, tab, newline that are in IFS) from the line.

Actual Behavior

[ a b ], [a][b ] and ne [ 42]. A single variable gets the line untouched, and the last of several variables keeps the trailing whitespace.

read with no name (REPLY) and IFS=; read x correctly keep the whitespace, same as bash.

Additional Context

src/builtins/read.rs: the single-variable branch assigns the whole line (proc.set_env(vars[0], &line)), and the multi-variable branch only trims the start of the remainder. Padded values then fail string comparisons, for example a count read from wc output.

With #307, read -d DELIM x trims trailing IFS whitespace but not leading (printf ' a\n' | read -d '' x gives a, bash gives a), so the same fix should cover that path.

Found while reviewing #307.

Possible Solution

When at least one name was given, trim leading and trailing characters that are both in IFS and one of space, tab or newline, before splitting. Leave REPLY untouched.

Related Issues

#307, #308

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