LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-30-2007, 10:14 AM   #1
johnpaulodonnell
Member
 
Registered: Jun 2006
Location: Galway
Distribution: Suse 10.1
Posts: 114

Rep: Reputation: 15
Do shell scripts permit arithmetic operations?


Hi.

In a language such as C or something you can define a variable to be of type double, say, and then go on and perform numerical operations involving this variable.

result=var*3.14 for example, where result would be defined to be of type double also.

Are such operations possible within shell scripts? I've defined a variable $norm to represent the number of seconds in a day in a bash script:

norm=24*60*60

But then just to check everything was as it should be I echoed the variable norm to the screen ( echo $norm ) and it gave me: > 24*60*60

I have a bit of simple multiplying and dividing to do involving variables...is this possible within shell scripts?

Thanks
 
Old 01-30-2007, 10:20 AM   #2
Intec
LQ Newbie
 
Registered: Jan 2007
Location: Brussels
Distribution: OpenSUSE
Posts: 7

Rep: Reputation: 0
Yes, you can use:
(( norm = 24*60*60 ))
or
let "norm=24*60*60"
 
Old 01-30-2007, 10:28 AM   #3
johnpaulodonnell
Member
 
Registered: Jun 2006
Location: Galway
Distribution: Suse 10.1
Posts: 114

Original Poster
Rep: Reputation: 15
Thanks for that.

But do you know if everything is treated as type double or what?

eg if a & b were defined as type int and were assigned:

a=5
b=2

then a/b=2 in other languages

Are there type definitions in scripting or is it handled automatically somehow?
 
Old 01-30-2007, 10:36 AM   #4
Intec
LQ Newbie
 
Registered: Jan 2007
Location: Brussels
Distribution: OpenSUSE
Posts: 7

Rep: Reputation: 0
Integers and strings only. You do not need a type definition:

darktown:~ # (( norm=5/2 ))
darktown:~ # echo $norm
2
darktown:~ # VAR="some text"
darktown:~ # echo $VAR
some text
darktown:~ # VAR=11/2
darktown:~ # echo $VAR
11/2
darktown:~ # (( VAR = 11/2 ))
darktown:~ # echo $VAR
5
darktown:~ # (( VAR = 11%2 ))
darktown:~ # echo $VAR
1

The % operator is for modulo.
If you need floating point, bash is not what you need.
 
Old 01-30-2007, 10:41 AM   #5
johnpaulodonnell
Member
 
Registered: Jun 2006
Location: Galway
Distribution: Suse 10.1
Posts: 114

Original Poster
Rep: Reputation: 15
ok. that's cleared it up. Thanks.
 
Old 01-30-2007, 11:15 AM   #6
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
bash doesn't have an internal mechanism to do floating point arithmetic. The $((expression)) syntax only does integer math.

You can always put a command through bc -l using the `backtick execution` syntax (or $(like this) if you prefer - as I do):
Code:
$ result=$(echo "3.14159265359 / 2" | bc -l)
$ echo $result
1.57079632679500000000
You could also switch shell to use zsh, which can do floating point arithmetic internally.
 
Old 01-30-2007, 11:24 AM   #7
anupamsr
Member
 
Registered: Sep 2004
Location: India
Distribution: Kubuntu, Archlinux, Suse, Gentoo, Mandrake
Posts: 370

Rep: Reputation: 30
Just to mention, you can use something like this too:
Code:
$ expr 1 + 1
2
Though this is specific to bash probably.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell scripts shmuel_orgad Programming 4 05-21-2006 06:33 AM
shell scripts newbie_m Linux - Newbie 3 01-18-2005 09:12 PM
shell scripts rocketgo Linux - General 10 12-01-2003 06:20 AM
shell scripts... roofy Linux - Newbie 13 05-06-2003 07:06 PM
need help on shell scripts!!! krcool32 Programming 5 04-28-2003 04:08 PM


All times are GMT -5. The time now is 08:27 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration