How in OS/X to get 'seq' command-line functionality?

老子叫甜甜 提交于 2019-12-06 02:42:49

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

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
Charles Duffy

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!