LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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, 05:06 AM   #1
hoi
LQ Newbie
 
Registered: Jun 2013
Posts: 27

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, 05:13 AM   #2
basica
Member
 
Registered: Nov 2011
Location: Australia
Distribution: Arch, LFS
Posts: 166

Rep: Reputation: 37
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, 05: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 05:30 AM.
 
1 members found this post helpful.
Old 09-02-2013, 05:42 AM   #4
eklavya
Member
 
Registered: Mar 2013
Posts: 619

Rep: Reputation: 136Reputation: 136
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, 05:53 AM   #5
hoi
LQ Newbie
 
Registered: Jun 2013
Posts: 27

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


Reply

Tags
bash script $@, linux command, shell script


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
"Power failure" at bash prompt - But no loss of power or UPS etc chrisphysics Linux - Kernel 4 08-05-2013 04: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 04: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 02:19 PM
copy directories and files recursively using C shell or bash shell bostonuser Programming 7 06-06-2008 02:24 AM


All times are GMT -5. The time now is 08:49 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration