Muffinresearch Labs by Stuart Colville

BASH Tip: Alternative to expr for arithmetic | Comments (2)

Posted in Snippets on 30th August 2007, 11:59 am by Stuart

I discovered recently a neat way to do arithmetic in BASH without the need to use the expr command. There’s a build-in that uses the syntax $((…)) or $[ … ]. This is also faster as it avoids the need to invoke a separate command. See: http://snap.nlc.dcccd.edu for more on the rules pertaining to this syntax.

Post Tools

Comments: Add yours

1. On September 2nd, 2007 at 5:34 am Kenan Bektas said:

This hint is totally cool.

Thanks,.

-Deniz Gezmis

2. On August 19th, 2008 at 4:55 pm akn said:

Hi,

Thanks for that tip. This was very useful i.e. to have a simplified replacement for the messy expr. In fact I didn’t know how to do compound statements using expr. For example how to do this expression (var1 – var2)*100/var1, since bash complains about those paranthesis. But using the other alternative I could easily compute the value as $[(var1-var2)*100/var2]. You don’t have to worry about escaping the *, + etc. in the expression. It is so easy to use! Thanks for the tip.







XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>



Using Loggerhead with mod_wsgi|(0)

Here’s a post I wrote over on the Project Fondue Blog about our use of Loggerhead with mod_wsgi under Apache. Loggerhead is the rather nice branch viewer for bazaar branches as used on Launchpad.net.

If you’re not already subscribed to the Project Fondue blog feed then I can recommend it, as there should be some interesting posts coming out of there in the coming months (yes I’m unashamedly biased!).

Ubuntu: Turn off changing workspace with mouse wheel|(1)

I found the changing with the workspace with the mouse wheel really annoying. To disable it go to System => Preferences => CompizConfig (available if the compizconfig-settings-manager package is installed) and uncheck “Viewport Switcher” which is under the “Desktop” heading.

Photos on Flickr

© Copyright 2004-10 Stuart Colville, all rights reserved. May contain traces of Muffin. Powered by WordPress. Hosting by Slicehost.com This page was baked in 0.588s.