seq for OS X

It has always irked me that OS X doesn’t have the seq command (I am easily irked). Brian Peterson’s old e-mail Re: seq from core utils has it, but the link to sh-utils doesn’t work any more, since the project has been archived. Here’s the new link: http://ftp.gnu.org/old-gnu/sh-utils/. Compile it as Brian suggested, and all will be well.

$ seq 1 12
1
2
3
4
5
6
7
8
9
10
11
12

Joy!
(at least 99% of you will be mystified why anyone would want this.)

21 comments

  1. .. or install macports and do a
    $ sudo port install coreutils
    which will get you a gseq:
    $ gseq 1 12
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12

  2. Yeah, but it’s called ‘seq’ in all the scripts I have, and it wouldn’t be worth me having macports for just one command.

  3. Hi, agreed, it bothers me too…. so much did i want this functionality without needing to do darwin ports … and having seen your recent post on this topic, i’ve just knocked together a 2 min bash script to add this functionality without actually installing anything,

    i just made this script and called it /usr/bin/seq
    chmod +x /usr/bin/seq
    and i’m good to go..

    … i also hope this posts ok…. i have a feeling it may get filtered by the forum… maybe you can figure it out again

    #!/bin/bash
    # marks rude seq replacement for osx.

    if [ $# -eq 2 ]
    then
    let from=”$1″;
    let to=”$2″;
    elif [ $# -eq 1 ]
    then
    let from=”0″;
    let to=”$1″;
    else
    echo “USAGE: seq [num from] [num to]”
    exit 1;
    fi

    while [ $from -lt $to ]
    do
    printf “$from “;
    from=$[$from+1]
    done
    printf “$from “;

  4. sudo ln -s /opt/local/bin/gseq /opt/local/bin/seq

    🙂

  5. Playing with jot a bit (I’m on a train), I rather like it. What jot can do in one command

    jot -w %02x 255 0

    (printing hexadecimal numbers 00 .. ff) seq needs a composite command:

    seq 0 255 | xargs -n1 printf “%02x\n”

    neat!

  6. Just an improvement of the bash script (which doesn’t yet work properly for jot with an increment). Further improvement is welcome !

    #!/bin/bash
    # marks rude seq replacement for osx.

    # set -ex

    method=1

    case ${method} in
    1)
    # a simple bash script:
    if [ $# -eq 2 ]
    then
    let from=”$1″;
    let to=”$2″;
    let step=1
    elif [ $# -eq 1 ]
    then
    let from=”0″;
    let to=”$1″;
    let step=1
    elif [ $# -eq 3 ]
    then
    let from=”$1″
    let to=”$3″
    let step=”$2″
    else
    echo “USAGE: seq [num from] [[num step]] [num to]”
    exit 1;
    fi

    while [ $from -le $to ]
    do
    printf “$from \n”;
    from=$[$from+$step]
    done
    ;;
    2)
    # Using the jot command:

    if [ $# -eq 2 ]
    then
    let numstep=`expr ‘(‘ $2 – $1 + 1 ‘)’ `
    jot $numstep $1 $2
    elif [ $# -eq 3 ]
    then
    let numstep=`expr ‘(‘ $3 – $1 + 1 ‘)’ / $2 `
    let to=`expr $3 – ‘(‘ ‘(‘ $3 – $1 + 1 ‘)’ % $numstep ‘)’ `
    jot ${numstep} $1 $to
    else
    echo “USAGE: seq [num from] [[num step]] [num to]”
    exit 1;
    fi
    ;;
    esac

  7. Thanks, but the seq builds are for PPC and not for intel.

    but the macports idea is good!

  8. If you’re using BASH (the default in OS X), try just using the curly braces BASH expansion:

    for i in {1..10}; do echo $i; done

  9. just install coreutils and then link to /usr/bin

    1. sudo port install coreutils
    2. which gseq
    3. sudo ln -s /opt/local/bin/gseq /usr/bin/seq

  10. err, installing macports and coreutils is a bunch of work. And I still stand by what I said about /opt – if not more so!

    I like the bash solution best of all, ‘cos I don’t have a single machine without it.

  11. The curly brackets doesn’ t seem to work on all platforms. i am using bash on 10.4 and it doesn’ t recognizes it. jot worked fine and I didn’ t have to install anything. Thanks for the tip!

  12. sudo port install coreutils
    sudo ln -sf /opt/local/bin/gseq /opt/local/bin/seq

Leave a comment

Your email address will not be published. Required fields are marked *