Skip to content

Comment on BASH as a Modern Programming Language [ppt]

Comments

Bash sucks at slicing arrays (always need a `[@]`):

    ${array[@]:$from:$to}
Zsh is a bit nicer:
    ${array:$from:$to}

In Bourne-descended shells, as an unfortunate historical accident, if FOO is an array, then referring to FOO without a subscript is equivalent to referring to its zeroth element.

For your example,

  ${array:$from:$to}
is equivalent to
  ${array[0]:$from:$to}
And likewise
  $ FOO=(a b c d)
  $ FOO=x
  $ echo ${FOO}
  x
  $ echo ${FOO[@]}
  x b c d
AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.