How to display numbers in reverse order using seq(1)?
use negative increment
seq -s, 10 -2 1
10,8,6,4,2
In general, you don't want to use seq
, it's not portable (even among standard Linux environments). If you're using ksh, zsh, or bash4+, you can use brace expansion:
echo {10..1..2} | tr " " ,
10,8,6,4,2
Another way in pure bash, ksh or zsh:
for ((i=10;i>0;i-=2)) ; do echo -n "$i," ; done
A pure POSIX sh way:
i=10
while [ "$i" -gt 2 ]; do printf "$i,"; i=$((i-2)); done
echo "$i"