Bash script variables assignment syntax question

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 variables assignment syntax question

#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?

musher0
Posts: 14629
Joined: Mon 05 Jan 2009, 00:54
Location: Gatineau (Qc), Canada

#2 Post by musher0 »

Hello!

I know it is never wrong to put a 'let' statement before this type of variable, although it takes an extra 4 bytes.

Best of luck.
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)

User avatar
sunburnt
Posts: 5090
Joined: Wed 08 Jun 2005, 23:11
Location: Arizona, U.S.A.

#3 Post by sunburnt »

Hi emily22; "Let" is kinda diminished, or depreciated as it were ( not used ).

I can`t remember the forum name of the competent member that showed me this:

Code: Select all

aa=23 ; bbb=35

((dd=$aa+$bbb))

echo $dd

jpeps
Posts: 3179
Joined: Sat 31 May 2008, 19:00

#4 Post by jpeps »

sunburnt wrote:Hi emily22; "Let" is kinda diminished, or depreciated as it were ( not used ).

I can`t remember the forum name of the competent member that showed me this:

Code: Select all

aa=23 ; bbb=35

((dd=$aa+$bbb))

echo $dd
works, but what's wrong with dd=$(($aa + $bbb)) ?

amigo
Posts: 2629
Joined: Mon 02 Apr 2007, 06:52

#5 Post by amigo »

'$((dd=$aa+$bbb))' would be the most portable among different shells. 'let' is bash-specific. While '((dd=$aa+$bbb))' works with bash, it may not with other shells. Whitespace around the operators has o effect.

musher0
Posts: 14629
Joined: Mon 05 Jan 2009, 00:54
Location: Gatineau (Qc), Canada

#6 Post by musher0 »

Hello.

Even if the "let" statements are officially deprecated, the following script does not work if the "let" statements are removed.

Code: Select all

#!/bin/sh
# ~/$MAPPS/bin/urxvt-fordo-cascade-4.sh
####
# Ce script-ci doit demeurer en l'état.
# (c) Christian L'Écuyer, Gatineau (Qc), Canada, janvier 2012. Gratuiciel, mais tous droits réservés.
# Courriel : http://scr.im/reyucelc
#
# Si ça ne marche pas, assurez-vous qu'un autre "rxvt" ne tourne pas quelque part en mémoire.
####
if [ "`pidof rxvt`" != "" ];then
	for i in `seq 4`;
	do
		wmctrl -c Console
	done
	exit
	else	
		export gm="g 80x16"
		let x=45
		let y=80
		for i in `seq 4`;
	        do
	                rxvt -bd grey10 -b 4 -fg white -cr green -sr +st +tcw -bc -tint black -sh 57 -T "Console $i" -$gm-$x-$y & 
			let x=$x+120
			let y=$y+170
			sleep 0.17s
	        done 
fi
Is it me, bash (version 4.1.0-1_i686, 2009), my computer, my Puppy version (lupu 5.25 retro4), my attitude towards life, a spell from my ex? :lol: Someone has an explanation? Thanks in advance.

Also, before you ask, I have found a reason to open 4 console windows at the same time! :D

BFN.
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)

User avatar
sunburnt
Posts: 5090
Joined: Wed 08 Jun 2005, 23:11
Location: Arizona, U.S.A.

#7 Post by sunburnt »

musher0;

Code: Select all

x=45 ; y=80
........
((x=$x+120)) ; ((y=$y+170))

Peterm321
Posts: 411
Joined: Thu 29 Jan 2009, 14:09
Location: UK

#8 Post by Peterm321 »

emily22 wrote:For example

aa=23
bbb=35
cccc=479

and I want to assign:

dd=$(($aa+$bbb))

Code: Select all

#!/bin/bash
aa=23
bbb=35
cccc=479 
let dd=aa+bbb
echo -e "\ndd= $dd\n"

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#9 Post by technosaurus »

musher0 wrote:Hello.

Even if the "let" statements are officially deprecated, the following script does not work if the "let" statements are removed.

...

Is it me, bash (version 4.1.0-1_i686, 2009), my computer, my Puppy version (lupu 5.25 retro4), my attitude towards life, a spell from my ex? :lol: Someone has an explanation? Thanks in advance.

Also, before you ask, I have found a reason to open 4 console windows at the same time! :D

BFN.
see amigo's post for portable, alternative syntax
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

Shep
Posts: 878
Joined: Sat 08 Nov 2008, 07:55
Location: Australia

#10 Post by Shep »

sunburnt wrote:I can`t remember the forum name of the competent member that showed me this:

Code: Select all

aa=23 ; bbb=35

((dd=$aa+$bbb))

echo $dd
Inside $((....)) bash allows you to omit the $ preface to all the variables:

a=24 b=56 c=$((a+b)); echo $c
80

musher0
Posts: 14629
Joined: Mon 05 Jan 2009, 00:54
Location: Gatineau (Qc), Canada

#11 Post by musher0 »

Thanks to all for their answers.
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)

User avatar
sunburnt
Posts: 5090
Joined: Wed 08 Jun 2005, 23:11
Location: Arizona, U.S.A.

#12 Post by sunburnt »

Shep; You`re right of course, I was winging it from memory.
So...
((dd=aa+bb))

jpeps
Posts: 3179
Joined: Sat 31 May 2008, 19:00

#13 Post by jpeps »

sunburnt wrote:Shep; You`re right of course, I was winging it from memory.
So...
((dd=aa+bb))
interesting; I'd never tried that before. Just goes to prove that $$ is dispensable.

Post Reply