hey there to all!
I'm writing a bash script and I wonder what is the correct syntax for arithmetic expression when it comes to integers which are stored in variables.
For example
aa=23
bbb=35
cccc=479
and I want to assign:
dd=$(($aa+$bbb))
or
aa=$(($aa+$bbb))
or
ee=$(($cccc-$aa))
Is this correct or let is working better?
Also do I have to leave white spaces between operands?
bash script
Why not test?
Correct!
Code: Select all
# aa=23; bbb=35; dd=$(($aa+$bbb)); echo $dd
58
yes you're right I just wanted to know if both formats are acceptablezigbert wrote:Why not test?Correct!Code: Select all
# aa=23; bbb=35; dd=$(($aa+$bbb)); echo $dd 58
and which one is more preferable!
- Moose On The Loose
- Posts: 965
- Joined: Thu 24 Feb 2011, 14:54
In general, I like to leave white spaces because it makes the expression easier to read. It doesn't seem to have any impact on speed.emily22 wrote:yes you're right I just wanted to know if both formats are acceptablezigbert wrote:Why not test?Correct!Code: Select all
# aa=23; bbb=35; dd=$(($aa+$bbb)); echo $dd 58
and which one is more preferable!
I also use the ${AA} form of the variable use even in cases where it is not needed if it makes the expression clearer.
When coding something biggish, I try to use names that make sense for what I am doing. Even though using $KBYTES takes more characters than $N, it is worth doing on something you have to maintain.