LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 09-02-2013, 04:06 AM   #1
hoi
Member
 
Registered: Jun 2013
Posts: 32

Rep: Reputation: Disabled
x to the power y in bash shell


echo "$a^$b" | bc -l
Where 'a' is a floating point and 'b' is integer.
If 'a' and 'b' are both floating point then above command does not work.
How to do when 'a' and 'b' both are floating point?
 
Old 09-02-2013, 04:13 AM   #2
basica
Member
 
Registered: Nov 2011
Location: Australia
Distribution: Arch, LFS
Posts: 171

Rep: Reputation: 38
Google brought up this wikipedia entry, seems like writing a function can take care of this in bc.
 
1 members found this post helpful.
Old 09-02-2013, 04:28 AM   #3
Berhanie
Senior Member
 
Registered: Dec 2003
Location: phnom penh
Distribution: Fedora
Posts: 1,625

Rep: Reputation: 165Reputation: 165
right, you can use "bc -l" and the fact that a ^ b = exp(b * log(a)). that's also mentioned in the link above.
example (computes the square root of 25):
Code:
echo "e(0.5 * l(25))" | bc -l

Last edited by Berhanie; 09-02-2013 at 04:30 AM.
 
1 members found this post helpful.
Old 09-02-2013, 04:42 AM   #4
eklavya
Member
 
Registered: Mar 2013
Posts: 636

Rep: Reputation: 142Reputation: 142
well if you want to use awk, you can use

Code:
echo "" | awk 'END {print "'${a}'" ^ "'${b}'" }'
So script will look like this.

Code:
#!/bin/bash
a=4.2
b=3.5
echo "" | awk 'END {print "'${a}'" ^ "'${b}'" }'
When you will run this, the output will be
151.835
 
Old 09-02-2013, 04:53 AM   #5
hoi
Member
 
Registered: Jun 2013
Posts: 32

Original Poster
Rep: Reputation: Disabled
Thanks to all for quick response....
 
  


Reply

Tags
bash script $@, linux command, shell script



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
"Power failure" at bash prompt - But no loss of power or UPS etc chrisphysics Linux - Kernel 4 08-05-2013 03:49 AM
ssh from solaris 9 with a bash shell of 2.0 to suse 3.2 bash shell slufoot80 Solaris / OpenSolaris 3 09-19-2012 03:06 PM
ssh from solaris 9 with a bash shell of 2.0 to suse 3.2 bash shell slufoot80 Solaris / OpenSolaris 1 09-19-2012 01:19 PM
copy directories and files recursively using C shell or bash shell bostonuser Programming 7 06-06-2008 01:24 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:06 PM.

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
Open Source Consulting | Domain Registration