-
-
Notifications
You must be signed in to change notification settings - Fork 152
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
Quoted brackets aren't matched by % and # parameter expansion #290
Comments
Thanks for the report! I'm not sure what's going on yet, but I discovered that bash behaves differently than other shells here. if var is
|
If var='[foo]', then we will get 'foo]'. This edge case is pretty confusing (even by shell standards.) For some reason, dash, mksh, and zsh disagree. They must treat [ as an invalid glob and leave it as '[foo]'? In other words, they require ${var%'['}. But bash and busybox ash agree, and the code seems to make sense -- i.e. we perform glob unescaping if the arg word doesn't look like a glob, because we already glob-escaped it. LooksLikeGlob() is necessarily a heuristic. Fixes issue #290.
OK I just fixed this. This case was pretty confusing! dash, mksh, and zsh actually agree on a different behavior. But bash and busybox ash agree, and that behavior seems to make more sense, so I made OSH agree with them. Thanks for the report -- keep them coming! |
Don't all shells agree on the reported issue (
Seems that single quoted brackets still aren't matched; this also doesn't work on / expansion. Also, I forgot to mention a similar problem: |
Yes that's true, OSH already disagreed with dash/mksh/zsh. I think the http://www.oilshell.org/release/0.6.pre18/test/spec.wwz/glob.html (compare cases 29-30 with 30-31) I haven't decided what to do here yet, but I don't consider it high priority until a real and unpatchable script uses it.
The fundamental reason for the difference is long and old... basically OSH uses libc pattern matching like But ksh originated In order to implement So OSH translates the So there are some inconsistencies there, particularly with regard to Unicode. It may be worth opening another bug about this, but like I said I usually put them at high priority only when a "real" script tickles the behavior. (This one seemed to be worth fixing simply because I was confused at the behavior, and it was a bug.) FWIW all shells have to jump through some hoops to implement ksh constructs... but most of them also have their own string matching algorithms, so it's not quite the same as the issue with OSH. I'm pretty sure |
Another possibility is to generate a runtime error for |
Ah, that makes sense. Other issues I've noticed are with array subscripts in arithmetic expansion (e.g., single-quoted/escape ones aren't expanded, associative arrays expand to the associative array in JSON or something), as well as
Yes, this one I just stumbled upon interactively; I use I'm not sure what you consider a "real" script, but all of the issues I've reported have been from actual Bash programs, though not very practical ones, except for the one I mentioned. I tried to patch them, but there were too many issues. I also tried an |
@Crestwave I don't recognize any of those bugs, except that I think the associative array issue may be fixed by #287. I looked at the past issues you've filed and I don't see them. Can you provide a repro for each one? Like a short code snippet, what OSH does, and what you think should happen? What I mean by "real" is something that people use, i.e. rather than a contrived test case. You pointed to some on #274. |
In particular I don't recall this, and I am not even familiar with that syntax ... ? |
Sure. Escaped/single-quoted variables in array subscripts in arithmetic expressions don't expand: $ i=0
$ arr[i]=0
$ (( ++arr['$i''] ))
Line 3 of '/home/user/.config/oil/oshrc'
(( ++arr['$i'] ))
^
osh warning: Invalid integer constant '$i' If you're wondering what use this has, it's usually done with associative arrays, as you have to use the For completeness, associative arrays expand to JSON when expanded by arithmetic expressions: $ declare -A arr
$ arr[0]=0
$ (( var = ${arr[0]} ))
$ (( var = arr[0] ))
*** Error has no source location info ***
fatal: Expected array in index expression, got {'0': '0'} Escape sequences are incorrectly handled when combining $ var=$'\n'
$ printf '%q\n' "${var#?}"
''
$ printf '%q\n' "${var%''}"
''$'\n'
$ printf '%q\n' "${var%"${var#?}"}"
'' Single-quotes are literal in quoted parameter expansion: $ var=a
$ echo ${var#'a'}
$ echo "${var#'a'}"
a
$ var="'a'"
$ echo "${var#'a'}"
Matching $ var=+-
$ echo "${var//['+-']}"
Line 16 of '/home/user/.config/oil/oshrc'
echo "${var//['+-']}"
^
fatal: Error matching regex "(['+-'])": Invalid regex syntax (func_regex_first_group_match)
The reports I've given are from actual programs I've tried, except for the one issue I mentioned, although this particular batch isn't exactly very popular: [sh] [bash].
It's simply single-quoted instead of double-quoted/escaped. |
Thanks, these are good test cases! I reproduced all of them as a difference between OSH and bash. bash and mksh seem to agree on most of them, but not all. Some of them may boil down to OSH's more static evaluation model, but I will think about them and either fix or document them after some hacking. Let me create some new bugs to discuss them in isolation. |
Reviewing the rest of the list you sent, I filed #294, that is indeed a bug. The case
is an intentional incompatibility, documented here: https://github.com/oilshell/oil/blob/master/doc/osh-manual.md http://www.oilshell.org/blog/2016/10/20.html That can be written There may two related issues there: the undecidable parse issue, and also the recursive arithmetic evaluation rule #3, which is also intentionally not implemented. Arithmetic expressions are only evaluated once unlike in ksh/bash. It's more predictable and also more secure. The ksh/bash behavior is like a silent Please file more issues as you find them! |
This fix is released! Latest version of OSH: http://www.oilshell.org/release/0.6.pre20/ |
The text was updated successfully, but these errors were encountered: