Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

FWIW, if you need to use `sh` instead of `bash` for some reason, you can do the range loop with:

    for i in $(seq 1 10); do echo $i; done


The seq command is not POSIX.

Only rely upon it when GNU is a focus, which is not everywhere.

These are the POSIX utilities:

https://pubs.opengroup.org/onlinepubs/9699919799/utilities/


Huh. Why did I misremember that?

I knew `seq` was available on BSDs. For some reason I thought it was on SVr4-derived Unices too (and therefore figured it was either actually Posix, or ubiquitous enough to be reliable anyway), but I guess I got that one wrong.

Maybe because GNU tends to be a bit more heavily influenced by SVr4 than BSD, historically?

Anyway, thanks for the correction.


If you're ok with integer math and no switches, you can easily make your own seq.

The next time that you are on an OSF/1 operating system, you can use this portable version. It's also probably faster than the fork() overhead, assuming dash.

  seq () {
    if [ -n "$3" ]
    then FIRST=$(($1 + 0)) INCREMENT=$(($2 + 0)) LAST=$(($3 + 0))
    else if [ -n "$2" ]
         then FIRST=$(($1 + 0)) INCREMENT=1 LAST=$(($2 + 0))
         else FIRST=1 INCREMENT=1 LAST=$(($1 + 0))
         fi
    fi
    c=$FIRST
    until [ $c -gt $LAST ]
    do printf %d\\n $c
       c=$((c + INCREMENT))
    done
  }


seq being available on BSDs doesn't mean either that it comes from POSIX.


Sorry, didn't mean to imply that.

The point I was making is that because (to my recollection) GNU tends to mimic SVr4, the fact that `seq` is in GNU is probably why I thought it was also available in SVr4-derived Unices.

But also, I know that `seq` is in the BSDs.

Therefore, if `seq` were in GNU, SVr4 and the BSDs, then a) the chances that it would have been included in Posix are very high, and b) even if it wasn't in Posix, the fact that it's in all 3 of those families of shell tools would make it ubiquitous enough that I'd be happy to rely on it for a personal project that I intended to be widely portable anyway.

(Posix tends to codify existing common practice, rather than designing new features for existing systems to implement.)




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: