How to count

When writing bash shell scripts, I keep forgetting how to do arithmetric.... so here is a reminder for me ....

#!/bin/bash

# Either :
f=10
let f=f+1
echo "$f is now 11"

# Or :
f=10
f=$(($f+1))
echo "$f is now 11"

# More advanced :

# (Random number between 0 and 8 inclusive)
f=`perl -le 'print int rand 9'` 
echo "$(($f+10)), $(($f+20)), $(($f+30)) prints e.g. 2, 14, 24, 24"

End memory jogging post.

Technorati Tags:

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img>
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
We don't take kindly to automated nonsensible adverts around here.