LinuxQuestions.org
Review your favorite Linux distribution.
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 04-25-2017, 05:41 PM   #1
jwildeILL
LQ Newbie
 
Registered: Apr 2017
Posts: 4

Rep: Reputation: Disabled
bash shell math


I think I am using expr wrong. I need to raise a number to the power of x.

I tried:

expr 3^5
expr 3'^'5
((3^5))

And they don't work
 
Old 04-25-2017, 06:11 PM   #2
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 21 MATE
Posts: 8,048
Blog Entries: 5

Rep: Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925
Hi jwildeILL.

I see that you have already marked this thread as "Solved".

What was your solution?
 
Old 04-25-2017, 06:15 PM   #3
jwildeILL
LQ Newbie
 
Registered: Apr 2017
Posts: 4

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by hydrurga View Post
Hi jwildeILL.

I see that you have already marked this thread as "Solved".

What was your solution?
echo '(3^5)' | bc

I think expr can only handle basic math like + - * and %
 
Old 04-25-2017, 06:21 PM   #4
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,264
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Welcome to LQ!

I do not think expr supports the bitwise-or operator, '^' (that is not exponent in the shell). See man expr to verify that.

expr is designed to be used inside command substitution and returns the value of the expression on exit.

Additionally, I do not think expr supports exponents, and you need to protect the * from the shell, so you would need to do something like this...

Code:
X=$(expr 3 \* 3 \* 3 \* 3 \* 3)
echo "$X"
From the book Classis Shell Scripting:

Code:
# Classic Shell Scripting - Oreilly, Section 7.6.3
# The expr command is one of the few Unix commands that is poorly designed and hard to use.
# Although standardized by POSIX, its use in new programs is strongly discouraged, since
# there are other programs and facilities that do a better job. In shell scripting, the major
# use of expr is for shell arithmetic, so that is what we focus on here. Read the expr(1)
# manpage if you're curious about the rest of what it can do.
#
# expr's syntax is picky: operands and operators must each be separate command-line arguments;
# thus liberal use of whitespace is highly recommended. Many of expr's operators are also
# shell metacharacters, so careful quoting is also required.
So, let's avoid using it unless you have some special reason to do so - there are better ways!

Your second method is much better, but it does not use the bash power operator (**), and it throws away the result without using it.

You need to use it to set some variable value, or better use it in command substitution context as well. Either of these will work:

Code:
((X=3**5))
echo "$X"

X=$((3**5))
echo "$X"
The first sets the value of the variable X, which you can then use as desired.

The second returns the value which is then placed into the variable X, which you may then use. Alternatively you could simply echo the returned value.

See the bash man page - it is long but very complete and helpful!

Good luck!

Last edited by astrogeek; 04-25-2017 at 06:33 PM. Reason: typo - everyone types faster than I do!
 
1 members found this post helpful.
Old 04-25-2017, 06:36 PM   #5
jwildeILL
LQ Newbie
 
Registered: Apr 2017
Posts: 4

Original Poster
Rep: Reputation: Disabled
Thanks astrogeek for the examples. +1
 
  


Reply



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
[SOLVED] math programming help in shell sniper8752 Linux - Newbie 3 10-15-2012 01:07 PM
Create a math script using the bash shell bigron1953 Linux - General 7 11-29-2011 12:25 PM
Doing math in bash? ugurgazi Programming 25 04-29-2011 05:42 AM
LXer: The Bash Shell: Doing Your Math LXer Syndicated Linux News 0 01-28-2011 01:50 PM
Loops in Bash shell or variable math... edpatterson Programming 4 10-29-2010 01:09 AM

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

All times are GMT -5. The time now is 03:43 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
Open Source Consulting | Domain Registration