Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Please could somebody explain how lines 3 and 8 work in this portion from a BASH script that I'm a bit confused with? It's to work out which USB drive to use for a backup. Many thanks.

1 WEEK_NO=`date +%U | sed -e 's/^0\+//g'`
3 (( USB_NN_IDX = $WEEK_NO % 2 ))
5 USB_DRIVES[0]="usb03"
6 USB_DRIVES[1]="usb04"
share|improve this question
Many thanks all. You've made me understand it now. – sixnumber Jun 28 '12 at 11:54
up vote 3 down vote accepted

On line 3 double parentheses are used in order to make sure that the expression between them is evaluated as arithmetic expression. $WEEK_NO % 2 returns the remainder of the division of $WEEK_NO by 2, so the result is always 0 or 1, depending on the $WEEK_NO (if it's even, result is 0; if it's odd, result is 1).

USB_DRIVES is an indexed array, and depending on the result of the evaluation on line 3, on line 8 one of its values is retrieved.

You can look at man 1 bash for more information. The relevant information is in the ARITHMETIC EVALUATION and Arrays sections.

share|improve this answer
Many thanks Vladimir Blaskov – sixnumber Jun 28 '12 at 11:30
(( USB_NN_IDX = $WEEK_NO % 2 ))

This one is used for calculation. It will let USB_NN_IDX equal to the remainder of an integer division operation variable WEEK_NO to 2.


this one will let USB_NN variable equal to one of USB_DRIVES[0] or USB_DRIVES[1] depending on USB_NN_INX value. You can use any implementation in bash to equal one variable to another from following:


both are allowed and last one is used here. Also in array you can use index as variable two.

share|improve this answer

The (( )) notation is a shorthand for let, read more here. So:

(( USB_NN_IDX = $WEEK_NO % 2 ))

Sets USB_NN_IDX to modulo 2 of week number, i.e. 0 or 1. This number is used as an index into the USB_NN array.

share|improve this answer

Line 3 ((...)) - is just an integer math embedded into shell. "%" - is a MODULO/REMINDER operator, complementary to the "/" that return integer part of division. In your case line 3 return 0 or 1 depends on week-number. Then that number substituted into device name USBx - USB_0 or USB_1.

Seems like backups written on a different flash-drives depends on even/odd week-number.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.