[lug] Letting Bash do the Arithmetic

Kenneth D. Weinert mc at quarter-flash.com
Tue Apr 15 13:28:45 MDT 2003


	It appears as if the (( )) syntax has to be a "primary"
operator - ie, it only works if the (( operator is the first one on
the line.

	This, however, does work (admittedly there's a cost to it):

head -n $(((23945837-4926402))) some.file

	So it just depends on whether the cost of spawning a shell to
do math is worth it :)

	Oh, just to point out two more possibilites:

	head -n $(echo "23945837 - 4926402"|bc) some.file
	head -n $(echo "23945837 4926402 - p"|dc) some.file

And I'm sure there are other variations with perl, etc, which are
obviously overkill for this particular application, but might come in
handy in other circumstances.

On Tue, Apr 15, 2003 at 01:17:24PM -0600, Sean Reifschneider wrote:
> On Mon, Apr 14, 2003 at 06:28:22PM -0600, Peter Hutnick wrote:
> >So I'm trying stuff like "head -n ((23945837-4926402)) some.file"  and
> >getting "bash: syntax error near unexpected token `('."

-- 
/~\ The ASCII        Ken Weinert   mc at quarter-flash.com 
\ / Ribbon Campaign  303-452-6603 (V) 303-705-4258 (F)
 X  Against HTML     GnuPG: 9274F1CE  GnuPG available at http://www.gnupg.org/
/ \ Email!           1D87 3720 BB77 4489 A928  79D6 F8EC DD76 9274 F1CE
2 + 2 = 5 for extremely large values of 2.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
URL: <http://lists.lug.boulder.co.us/pipermail/lug/attachments/20030415/331fb6f5/attachment.pgp>


More information about the LUG mailing list