According to the open group POSIX awk supports BEGIN
, therefore it can be done in awk
:
awk -v MYEND=6 'BEGIN { for(i=1;i<=MYEND;i++) print i }'
Where -v MYEND=6
would stand for the assignment as in the first argument to seq
. In other words, this works too:
END=6
for i in `awk -v MYEND=$END 'BEGIN { for(i=1;i<=MYEND;i++) print i }'`; do
echo $i
done
Or even with three variables (start, increment and end):
S=2
I=2
E=12
for i in `awk -v MYS=$S -v MYI=$I -v MYE=$E 'BEGIN { for(i=MYS;i<=MYE;i+=MYI) print i }'`; do
echo $i
done
Extra Solaris note: On Solaris /usr/bin/awk
is not POSIX compliant, you need to use either nawk
or /usr/xpg4/bin/awk
on Solaris.
On Solaris, you probably want to set /usr/xpg4/bin
early in PATH if you are running a POSIX compliant script.
Reference answer:
env
sh-bang line that pulls in something other than /bin/sh