Let's we define two variables:
If you want to get total value, you can do:
TOTAL_FILES=$((FILES_ETC+FILES_VAR)) echo $TOTAL_FILES
For 100% compatibility with old shells, you can use external command expr. In modern systems (ec. Centos 7 64bit), in fact, expr is not an external command. It is a internal shell function. So there is not significal difference in speed between above and this command:
TOTAL_FILES=`expr $FILES_ETC + $FILES_VAR` echo $TOTAL_FILES
Funny thing is that there must be spaces between arguments. In fact, expr is command, $FILES_ETC is the first argument, + is the second argument and $FILE_VAR is the third argument.
If you want to add numbers with decimal points, you can use bc. bc is very useful external command for making arithmetical operations:
TOTAL_FILES=$(echo $FILES_ETC + $FILES_VAR | bc) echo $TOTAL_FILES
Common mistakes are spaces before or after '=' sign. However bc and expr command expect spaces between arguments and operators, so in these case don't forget to spaces.
Was this information helpful to you? You have the power to keep it alive.
Each donated € will be spent on running and expanding this page about UNIX Shell.
We prepared for you video course Marian's BASH Video Training: Mastering Unix Shell, if you would like to get much more information.
Thank you. Marian Knezek