Hacker News new | ask | show | jobs
by fwip 2209 days ago
Anyone used to command-line tools is used to fields being 1-indexed.

awk uses $0 as the whole line, and $1 as the first field. cut uses -f1 as the first field $1 is the first argument to a posix shell script /1 is the first matched reference in a sed $1 is the first regex match in perl

A command-line tool being 0-indexed breaks from expectation of what everybody is used to using on the command line.

2 comments

Anyone is a generalization based on a narrow viewpoint. I would say I am pretty used to command-line tools at this point, at least enough to be using linux as a daily driver comfortably. And I frankly I didn't know 1-indexed fields were the norm, even though I knew $1 is the first argument to a poxis shell script (I always assumed $0 referred to the script or command itself).
That's fair, I overgeneralized.
I get what you're saying, but to be more general I'd argue that's just 0 indexing with the API specifying what's in what index.

I'd more "formally" define 0/1 indexing as:

Zero indexing: arr[0] is a valid way to address the first element of an array, and len(arr) - 1 is the index to the final element.

One indexing: arr[0] results in an error or an out of bounds access, and len(arr) is the index to the final element.

These statements are true in Matlab, but not most command line tools.