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

[word_eval] Fix shifted slicing of ${@:offset:length}. #658

Merged

Conversation

akinomyoga
Copy link
Collaborator

This is a quick fix for the third item in #653 (comment). I tried to make the change small, so it might not be so elegant.

Note: dash and mksh fail in test/spec.sh word-split for newly added tests because they don't support ${@:offset:length}.

@andychu
Copy link
Contributor

andychu commented Mar 15, 2020

Oh man I didn't know this -- have you seen it documented anywhere?

This is confusing to me:

$ set -- 1 2 3; echo ${@:2}
2 3
$ a=(1 2 3); echo ${a[@]:2}
3

But apparently zsh agrees with bash! So I guess we should do it... (if multiple shells agree on a behavior I take that as an argument for it.) zsh is usually the one that disagrees the most.

I see what you're doing, but let me think if there's a simpler way...

@andychu
Copy link
Contributor

andychu commented Mar 15, 2020

Wow this is also confusing:

$ set -- 1 2 3; echo ${@};
1 2 3
$ set -- 1 2 3; echo ${@:0};
-bash 1 2 3

You can get more items by adding index 0 !!!

@akinomyoga
Copy link
Collaborator Author

Oh man I didn't know this -- have you seen it documented anywhere?

I think I remember that it is explained by a simple one sentence in the manual. But today I found in the manual a detailed description with examples. Maybe the description is added recently.

From Shell Parameter Expansion (Bash Reference Manual)

If parameter is ‘@’, the result is length positional parameters beginning at offset. A negative offset is taken relative to one greater than the greatest positional parameter, so an offset of -1 evaluates to the last positional parameter. It is an expansion error if length evaluates to a number less than zero.

The following examples illustrate substring expansion using positional parameters:

$ set -- 1 2 3 4 5 6 7 8 9 0 a b c d e f g h
$ echo ${@:7}
7 8 9 0 a b c d e f g h
$ echo ${@:7:0}

$ echo ${@:7:2}
7 8
$ echo ${@:7:-2}
bash: -2: substring expression < 0
$ echo ${@: -7:2}
b c
$ echo ${@:0}
./bash 1 2 3 4 5 6 7 8 9 0 a b c d e f g h
$ echo ${@:0:2}
./bash 1
$ echo ${@: -7:0}

@andychu andychu merged commit d469d8f into oils-for-unix:master Mar 15, 2020
@akinomyoga akinomyoga deleted the fix-positional-parameter-slice branch March 15, 2020 04:24
@andychu
Copy link
Contributor

andychu commented Mar 15, 2020

Thanks a lot! I merged and moved the test cases to var-op-slice (renamed from var-op-other).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants