3

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.

2
  • Does for i in {1..100} work to you?
    – fedorqui
    Nov 20, 2013 at 16:25
  • 1
    You can probably use $(command) rather than the backtick syntax that I'm too lazy to figure out how to show in a comment. It's a bit easier to read, and it can be nested. Nov 20, 2013 at 16:49

4 Answers 4

3

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
2
  • It looks like you're on a newer version of OS/X. I get "seq command not found," however, the {1..10} works! Thanks! Nov 20, 2013 at 16:46
  • OS X 10.5 does not have seq. OS X 10.5 was the last version for the PowerMac's (PowerPC). Snow Leopard is OS X 10.6. It was available for Intel's.
    – jww
    Mar 9, 2016 at 22:56
3

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
5
  • Is jot standards-compliant? (Granted, seq isn't either, but it'd be nice to point folks towards tools that will be available wherever their shell is). Nov 20, 2013 at 16:52
  • I don't think there is a standards-compliant answer; brace expansion and C-style loops are absent from POSIX. jot is nice to know, as it's the seq-equivalent program available in OS X 10.6 (seq itself ships with 10.8+, maybe 10.7 as well). Anyway, the accepted answer and yours covers the bash options well enough.
    – chepner
    Nov 20, 2013 at 17:33
  • I do cover the POSIX option near the bottom of my answer; it's the third code block. Nov 20, 2013 at 19:15
  • 1
    For the curious. jot is a BSD utility. Available on all BSD systems and whatever borrowed from that (such OSX). AFAIK it's not available on any other UNIX system (Solaris, AIX, etc) or Linux, although it can often be installed as a package. It is somewhat unfortunate that the GNU folk created seq(1), instead of adopting/duplicating jot, which had already been around for a number of years... Feb 10, 2014 at 6:20
  • I can't seem to get jot to count backwards in steps of 0.1. It results in jot: bad precision value. Does it allow increments/decrements of decimal values? The man pages don't seem to be clear about it.
    – jww
    Mar 9, 2016 at 23:18
2

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.

6
  • i=20.0; while [ $(( ( i -= 0.1 ) >= 1.0 )) -ne 0 ]; results in syntax error: invalid arithmetic operator (error token is ".0"). Bash is GNU bash, version 3.2.17(1)-release (powerpc-apple-darwin9.0).
    – jww
    Mar 9, 2016 at 23:01
  • @jww, of course it does; bash has only integer math. There's no guarantee that seq provides floating-point either -- on several platforms it doesn't. Mar 9, 2016 at 23:01
  • But seq allows us to do it: for i in $(seq -f "%.1f" 20.0 -0.1 1.0).
    – jww
    Mar 9, 2016 at 23:02
  • @jww, not all versions of seq. (It's not a standardized command, so this should be no surprise). Mar 9, 2016 at 23:02
  • ...though looking at common implementations, that is more widely available functionality than I would have guessed. If there were significant call for it, the right tool to use in emulating it would probably be awk. Mar 9, 2016 at 23:04
0

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.

2
  • 1
    Using [ $1 > $2 ] is wrong here -- it's actually a redirection (to the file named in $2), not a math comparison. Use (( $1 > $2 )) instead, since you're already assuming bash-only syntax, or [ "$1" -gt "$2" ] for something more portable. Mar 9, 2016 at 23:05
  • 1
    ...and speaking of bash-only syntax, consider avoiding the function keyword. seq() { is the POSIX-defined way to declare functions, and thus a better habit to be in if you need to write portable code. Mar 9, 2016 at 23:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.