Question

I'm still on Snow Leopard (I know...) so forgive if this is fixed in one of the later versions of OS/X, but I want to do standard "seq" aka:

for i in `seq 1 100` ; do
  cat /whatever > $i.txt ;
done

I thought installing GNU tools would do it, but apparently not.

Was it helpful?

Solution

On my mac both of these work (OS X 10.8.5)

Andreas-Wederbrands-MacBook-Pro:~ raven$ for i in {1..10}; do echo $i; done
1
2
3
4
5
6
7
8
9
10
Andreas-Wederbrands-MacBook-Pro:~ raven$ for i in `seq 1 10`; do echo $i; done
1
2
3
4
5
6
7
8
9
10

OTHER TIPS

In Snow Leopard, you can use the jot command, which can produce sequential data like seq (and more, see the man page for details).

$ jot 5
1
2
3
4
5
$ jot 3 5
5
6
7

No need for a tool such as seq -- bash (like ksh and zsh) has syntax built-in:

# bash 3.x+
for ((i=0; i<100; i++)); do
  ...
done

...or, for bash 2.04+, zsh, and ksh93:

i=0; while ((i++ <= 100)); do
   ...
done

...or, for absolutely any POSIX-compliant shell:

while [ $(( ( i += 1 ) <= 100 )) -ne 0 ]; do 
  ...
done

bash also supports expansions such as {0..100}, but that doesn't support variables as endpoints, whereas the for-loop syntax is more flexible.

Or, just add this to your bash profile:

function seq {
  if [ $1 > $2 ] ; then
    for ((i=$1; i<=$2; i++))
      do echo $i
    done
  else
    for ((i=$1; i>=$2; i--))
      do echo $i
    done
  fi
}

It's not that hard.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top