Arithmetic Expansion
noun
say it: “uh-RITH-muh-tik ek-SPAN-shun” /əˈrɪθmətɪk ɪkˈspænʃən/
A way of asking the shell to work out a whole-number sum, such as 12 * 2 + 1, and use the answer.
What is arithmetic expansion?
Arithmetic expansion, $(( )), works out a sum with whole numbers and puts the answer in its place. Names inside it need no $. Dividing drops what's left over, % gives the remainder, and a decimal point is an error.
guests=12
echo $(( guests * 2 + 1 )) # 25
echo $(( 17 / 5 )) $(( 17 % 5 )) # 3 2
In a sentence
- “$(( 17 / 5 )) is 3, because arithmetic expansion works in whole numbers.”
- “With arithmetic expansion, the script works out how many cupcakes to buy.”
Level: Apprentice · More Unix terms at this level
Updated