bash script

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
emily22
Posts: 3
Joined: Sun 06 May 2012, 08:59

bash script

#1 Post by emily22 »

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?

User avatar
zigbert
Posts: 6621
Joined: Wed 29 Mar 2006, 18:13
Location: Valåmoen, Norway
Contact:

#2 Post by zigbert »

Why not test?

Code: Select all

# aa=23; bbb=35; dd=$(($aa+$bbb)); echo $dd
58
Correct!

emily22
Posts: 3
Joined: Sun 06 May 2012, 08:59

#3 Post by emily22 »

zigbert wrote:Why not test?

Code: Select all

# aa=23; bbb=35; dd=$(($aa+$bbb)); echo $dd
58
Correct!
yes you're right I just wanted to know if both formats are acceptable
and which one is more preferable!

User avatar
Moose On The Loose
Posts: 965
Joined: Thu 24 Feb 2011, 14:54

#4 Post by Moose On The Loose »

emily22 wrote:
zigbert wrote:Why not test?

Code: Select all

# aa=23; bbb=35; dd=$(($aa+$bbb)); echo $dd
58
Correct!
yes you're right I just wanted to know if both formats are acceptable
and which one is more preferable!
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.

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.

Post Reply