| Author |
Message |
emily22
Joined: 06 May 2012 Posts: 3
|
Posted: Sun 06 May 2012, 05:06 Post subject:
Bash script variables assignment syntax question |
|
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?
|
|
Back to top
|
|
 |
musher0

Joined: 04 Jan 2009 Posts: 2219 Location: Gatineau (Qc), Canada
|
Posted: Sun 06 May 2012, 18:57 Post subject:
|
|
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.
_________________
"To err is human; to really foul things up, you need a computer!" / "L'erreur est humaine; pour vraiment f... la m..., il faut un ordinateur." (Carleton University, banderole à la Rentrée 1979 / banner, start of 1979 school year) 
|
|
Back to top
|
|
 |
sunburnt

Joined: 08 Jun 2005 Posts: 4006 Location: Arizona, U.S.A.
|
Posted: Sun 06 May 2012, 23:50 Post subject:
|
|
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: | aa=23 ; bbb=35
((dd=$aa+$bbb))
echo $dd |
|
|
Back to top
|
|
 |
jpeps
Joined: 31 May 2008 Posts: 2422
|
Posted: Mon 07 May 2012, 00:56 Post subject:
|
|
| 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: | aa=23 ; bbb=35
((dd=$aa+$bbb))
echo $dd |
|
works, but what's wrong with dd=$(($aa + $bbb)) ?
|
|
Back to top
|
|
 |
amigo
Joined: 02 Apr 2007 Posts: 1759
|
Posted: Mon 07 May 2012, 03:30 Post subject:
|
|
'$((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.
|
|
Back to top
|
|
 |
musher0

Joined: 04 Jan 2009 Posts: 2219 Location: Gatineau (Qc), Canada
|
Posted: Tue 15 May 2012, 01:22 Post subject:
|
|
Hello.
Even if the "let" statements are officially deprecated, the following script does not work if the "let" statements are removed.
| Code: |
#!/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? 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!
BFN.
_________________
"To err is human; to really foul things up, you need a computer!" / "L'erreur est humaine; pour vraiment f... la m..., il faut un ordinateur." (Carleton University, banderole à la Rentrée 1979 / banner, start of 1979 school year) 
|
|
Back to top
|
|
 |
sunburnt

Joined: 08 Jun 2005 Posts: 4006 Location: Arizona, U.S.A.
|
Posted: Tue 15 May 2012, 19:26 Post subject:
|
|
musher0;
| Code: | x=45 ; y=80
........
((x=$x+120)) ; ((y=$y+170)) |
|
|
Back to top
|
|
 |
Peterm321
Joined: 29 Jan 2009 Posts: 196
|
Posted: Tue 15 May 2012, 20:10 Post subject:
|
|
| emily22 wrote: | For example
aa=23
bbb=35
cccc=479
and I want to assign:
dd=$(($aa+$bbb))
|
| Code: |
#!/bin/bash
aa=23
bbb=35
cccc=479
let dd=aa+bbb
echo -e "\ndd= $dd\n"
|
|
|
Back to top
|
|
 |
technosaurus

Joined: 18 May 2008 Posts: 3843
|
Posted: Tue 15 May 2012, 23:28 Post subject:
|
|
| 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? 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!
BFN. | see amigo's post for portable, alternative syntax
_________________ Puppy Web Desktop Now with pet packages - Pet Packaging 100 & 101
|
|
Back to top
|
|
 |
Shep
Joined: 08 Nov 2008 Posts: 840 Location: GIRT-BY-SEA
|
Posted: Sun 20 May 2012, 10:54 Post subject:
|
|
| sunburnt wrote: | I can`t remember the forum name of the competent member that showed me this:
| Code: | 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
|
|
Back to top
|
|
 |
musher0

Joined: 04 Jan 2009 Posts: 2219 Location: Gatineau (Qc), Canada
|
Posted: Sun 20 May 2012, 15:03 Post subject:
|
|
Thanks to all for their answers.
_________________
"To err is human; to really foul things up, you need a computer!" / "L'erreur est humaine; pour vraiment f... la m..., il faut un ordinateur." (Carleton University, banderole à la Rentrée 1979 / banner, start of 1979 school year) 
|
|
Back to top
|
|
 |
sunburnt

Joined: 08 Jun 2005 Posts: 4006 Location: Arizona, U.S.A.
|
Posted: Sun 20 May 2012, 18:27 Post subject:
|
|
Shep; You`re right of course, I was winging it from memory.
So...
|
|
Back to top
|
|
 |
jpeps
Joined: 31 May 2008 Posts: 2422
|
Posted: Sun 20 May 2012, 19:23 Post subject:
|
|
| sunburnt wrote: | Shep; You`re right of course, I was winging it from memory.
So...
|
interesting; I'd never tried that before. Just goes to prove that $$ is dispensable.
|
|
Back to top
|
|
 |
|