Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: read -n strips leading and trailing whitespace #1937

Open
glyh opened this issue Apr 9, 2024 · 4 comments
Open

Bug: read -n strips leading and trailing whitespace #1937

glyh opened this issue Apr 9, 2024 · 4 comments

Comments

@glyh
Copy link
Collaborator

glyh commented Apr 9, 2024

Script a:

#!/usr/bin/env ysh

var block_size = 4
var EOF=']]]'
while (true) {
  read -n $block_size chunk
  write -n $chunk$EOF
}

script b:

#!/usr/bin/env bash

EOF=']]]'
while true
do
  read -n4 chunk
  echo -n $chunk$EOF
  sleep 1
done

test:

$ echo 'I am a pretty long string' |  ./a
I am]]]a p]]]rett]]]y lo]]]ng s]]]trin]]]g]]]    read -n $block_size chunk
    ^~~~
./a:6: errexit PID 501165: command.Simple failed with status 1
$ echo 'I am a pretty long string' |  ./b
I am]]]a p]]]rett]]]y lo]]]ng s]]]trin]]]g]]]]]]]]]]]]]]]]]]]] (omitted, bash keeps hanging forever even if the stdin to read is closed) 
@glyh
Copy link
Collaborator Author

glyh commented Apr 9, 2024

Was bitten by this when testing channels.

@glyh glyh self-assigned this Apr 9, 2024
@andychu
Copy link
Contributor

andychu commented Apr 9, 2024

Hmm actually this is an Oils bug only? What's wrong with bash?

andy@andy-VirtualBox:~/git/oilshell/oil$ bash -c 'echo " a b" |(read -n 4; echo "$REPLY")'
 a b
andy@andy-VirtualBox:~/git/oilshell/oil$ bin/osh -c 'echo " a b" |(read -n 4; echo "$REPLY")'
a b

Great find, thank you

I believe it might be related to stripping leading and trailing whitespace

@andychu
Copy link
Contributor

andychu commented Apr 9, 2024

andy@andy-VirtualBox:~/git/oilshell/oil$ bash -c 'echo "  a b  " |(read -n 7; echo "[$REPLY]")'
[  a b  ]
andy@andy-VirtualBox:~/git/oilshell/oil$ bin/osh -c 'echo "  a b  " |(read -n 7; echo "[$REPLY]")'
[a b]

@andychu andychu changed the title [ BUG ] both bash and ysh can't read string input correcty with read -n when input contains spaces Bug: read -n strips leading and trailing whitespace Apr 9, 2024
@andychu andychu added the bug label Apr 9, 2024
andychu pushed a commit that referenced this issue Apr 9, 2024
@andychu
Copy link
Contributor

andychu commented Apr 9, 2024

OK I wrote some failing spec tests

I think there are multiple things going on here, let me start a Zulip thread

andychu pushed a commit that referenced this issue Apr 9, 2024
andychu pushed a commit that referenced this issue Apr 9, 2024
This affects

    read vs read myvar
    read -r vs read -r myvar
    read -n 99 vs read -n 99 myvar

Shells disagree about this!  But we're following bash.

This is issue #1937
andychu pushed a commit that referenced this issue Apr 10, 2024
As a YSH style API for read -n, which takes an optional value.Place.

Updated doc/ref, and doc/idioms

Inspired by issue #1937.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants