LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 03-27-2008, 12:49 PM   #1
lumix
Member
 
Registered: Mar 2007
Distribution: Hardy (Gnome on Ubuntu 8.04) on Compaq N600c laptop
Posts: 323

Rep: Reputation: 30
How to multiply with expr? ( expr 3 * 4 doesn't work, though expr 3 + 4 does)


I've tried many combinations and googled this ad nauseam, but I keep getting a syntax error on multiply only. I've also tried using a bash shell.

Ultimately, I want to assign a number to $x, then multiply $x * 25, for example. Can't do it.
 
Old 03-27-2008, 01:00 PM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Multiplication with expr is done by \*, e.g.
Code:
y=$(expr $x \* 5)
 
Old 03-27-2008, 01:43 PM   #3
lumix
Member
 
Registered: Mar 2007
Distribution: Hardy (Gnome on Ubuntu 8.04) on Compaq N600c laptop
Posts: 323

Original Poster
Rep: Reputation: 30
Wow...okay. Huge thanks.

Why didn't that come up at ALL in dozens of google search results? Don't get me wrong, it worked (in bash), but it seems strange that it didn't come up.
 
Old 03-27-2008, 02:11 PM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Indeed it is poorly documented, but the first place to look at is the man page. Here is an excerpt from 'man expr'
Quote:
Beware that many operators need to be escaped or quoted for shells.
The second place to look at when facing shell issues is the Advanced Bash Scripting Guide, where in the description of the expr command you find
Quote:
expr 5 \* 3 returns 15
The multiplication operator must be escaped when used in an arithmetic expression with expr.
 
Old 02-25-2009, 09:30 AM   #5
aeronaut
LQ Newbie
 
Registered: Feb 2009
Posts: 1

Rep: Reputation: 0
Man pages

Colucix,

Thanks. I also had problems with this "feature".

Why don't the man pages spell this out in an example. +, -, / and % all work as expected, so the idea that we need to use \* to multiply doesn't come across well at all. Both the supercomputer at ANL and my own mac have man pages for expr, slightly different, and neither makes this clear. A simple

prompt>expr 2 /* 3
6

would make this quite transparent.

Regards,
aeronaut
 
Old 02-25-2009, 05:31 PM   #6
AlucardZero
Senior Member
 
Registered: May 2006
Location: USA
Distribution: Debian
Posts: 4,824

Rep: Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615
* is expanded by your shell. You have to pass the * to the program instead of your shell, escaping it with \ does that.
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
expr question sporty Linux - Newbie 2 03-22-2006 04:15 PM
Expr in Shell mystical_sun2000 Linux - Newbie 1 02-08-2006 06:59 AM
About how to use expr naihe2010 Programming 2 10-31-2005 06:28 AM
simple expr izza_azhar Programming 3 01-17-2005 07:38 PM
EXPR in shell pragti Programming 3 07-21-2004 03:54 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 08:01 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