Comments (6)
Interesting! Thanks for looking in to this. I didn't know about shvar
. For now I can work around the issue with a loop like this.
proc p {
var line = ""
while true {
try { shvar IFS= { read -r line } }
if (_status !== 0) {
break
}
write $line
}
}
I just recently started learning YSH, and it's been great so far. Love the project!
from oil.
Hmmm I just reproduced this ...
Let me look into this, thanks for the clear report!
from oil.
Hm funny that it works in proc
without IFS=
(though removing IFS= does change the behavior, even for one read arg!)
But I think there's still a bug here. I think it is probably related to lack of dynamic scope in procs
Our replacement for dynamic scope is shvar IFS= { read -r line }
, but that is actually problematic in loops, since it's a command
I'll look into it a little more ... hm
from oil.
OK wow, now I see the problem...
It's because
-
IFS= anycommand
creates a new stack frame in shell. That's how "temp bindings" are implemented in all shells -
In OSH shell functions,
read -r line
uses dynamic scope. This means it often creates a global variable, unless you declare withlocal
orvar
inside the function
$ f() { read -r zz; echo "inside f: $zz"; }; f <<< 'stdin'; echo "global: $zz"
inside f: stdin
global: stdin
Actually I was confused about this for a long time ... I thought that read -r line
would logically modify the calling stack frame! But it doesn't -- it looks up the whole stack, and if nothing is there, it creates a global
- YSH
proc
don't have dynamic scope. We got rid of that because it's unfamiliar to say Python and JS programmers
So basically YSH creates the line
variable in the temp binding, and it gets immediately thrown away when read
returns !!
So bottom line:
I will add
read --bytes-of-line
as a synonym forIFS= read -r
- rationale on Zulip (please join if interested) - https://oilshell.zulipchat.com/#narrow/stream/417617-help-wanted/topic/for.20loop.20that.20buffers.20lines
- related deprecation related to buffering - https://www.oilshell.org/blog/2024/03/release-0.21.0.html#breaking-changes
And then let me think about what we can do about this IFS= read
pitfall
It will also affect mapfile
and any other builtins which set variables
Hm hm
from oil.
I implemented
read --raw-line
for unbuffered readfor line in <> { echo $line }
for buffered read
So you don't have to use the old shell idioms
Documented here
I guess I will turn this bug into "do something about IFS= read -r` in YSH ... that is confusing
Maybe we need a lint rule or something
from oil.
Related to
from oil.
Related Issues (20)
- Setting up GNU readline history HOT 1
- 'view latest' released version in documentation - because old links are often surfaced HOT 1
- crash in parsing return HOT 1
- BashArray could have a sparse representation (ble.sh)
- bash SHELLOPTS and BASHOPTS support
- Fatal error when closing stderr on a non-fatal error
- nixos defaultUserShell HOT 4
- `json read` unexpectedly parses `123\x00` HOT 8
- Sudo in dev builds isn't very necessary HOT 2
- duplicate error messages in redirects HOT 1
- segfault on MacOS - maybe related to case statement HOT 2
- define/document control flow out of eval, eval(), blocks, subprocesses (pipelines, subshells)
- osh hangs after nano and sometimes other outputs HOT 3
- tool to print sourced files - dynamic tree shaking
- Hay: Code nodes containing blocks don't serialize code correctly HOT 4
- trap INT doesn't run on Ctrl-C
- Trap does not check for the first argument being an unsigned integer
- can't Ctrl-C out of long operation (regex)
- osh pwd and ls disagree on current dir
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from oil.