Skip to content

Commit 2b67988

Browse files
author
Andy C
committed
[osh] Change spec to allow ${arr[@]::}, implement it in OSH
The the missing length is interpreted as zero. Even though - It appears undocumented - zsh doesn't agree This came up on PR #725 a few years ago, for ble.sh Discussion: https://oilshell.zulipchat.com/#narrow/stream/121540-oil-discuss/topic/.24.7Barr.5B.40.5D.3A.3A.7D.20in.20bash.20-.20is.20it.20documented.3F
1 parent 1b0993c commit 2b67988

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

osh/word_parse.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -286,10 +286,11 @@ def _ReadSliceVarOp(self):
286286
if self.token_type != Id.Arith_RBrace:
287287
length = self._ReadArithExpr(Id.Arith_RBrace)
288288
else:
289-
p_die('Use explicit slice length of zero',
290-
self.cur_token)
291-
# bash behavior
292-
# length = arith_expr.EmptyZero # ${a:1:} or ${a::}
289+
# quirky bash behavior:
290+
# ${a:1:} or ${a::} means zero length
291+
# but ${a:1} or ${a:} means length N
292+
length = arith_expr.EmptyZero
293+
293294
return suffix_op.Slice(begin, length)
294295

295296
elif cur_id == Id.Arith_RBrace: # ${a:1} or ${@:1}

spec/var-op-slice.test.sh

+17-7
Original file line numberDiff line numberDiff line change
@@ -367,22 +367,32 @@ $SH -c 's=123; argv.py space ${s: }'
367367
['space', '123']
368368
## END
369369

370-
#### don't agree with ${array[@]::} has implicit length of zero!
370+
#### ${array[@]::} has implicit length of zero - for ble.sh
371+
372+
# https://oilshell.zulipchat.com/#narrow/stream/121540-oil-discuss/topic/.24.7Barr.5B.40.5D.3A.3A.7D.20in.20bash.20-.20is.20it.20documented.3F
373+
371374
array=(1 2 3)
372375
argv.py ${array[@]::}
376+
argv.py ${array[@]:0:}
377+
378+
echo
373379

374380
set -- 1 2 3
375-
#argv.py ${@:}
376381
argv.py ${@::}
382+
argv.py ${@:0:}
377383

378-
## status: 1
379-
## stdout-json: ""
384+
## status: 0
385+
## STDOUT:
380386

381-
## OK osh status: 2
387+
## status: 0
388+
## STDOUT:
389+
[]
390+
[]
382391

383-
## N-I bash status: 0
384-
## N-I bash STDOUT:
385392
[]
386393
[]
387394
## END
388395

396+
## OK mksh/zsh status: 1
397+
## OK mksh/zsh STDOUT:
398+
## END

0 commit comments

Comments
 (0)