Expr Syntax Error Multiply
Contents |
Search Tutorials/Articles Search HCL Search Reviews Search ISOs Go to Page... LinuxQuestions.org > Forums > Linux Forums > Linux - General expr syntax error multiplication How to multiply with expr? ( expr 3 * 4 doesn't expr syntax error in shell script work, though expr 3 + 4 does) User Name Remember Me? Password Linux - General This Linux forum
Bash Expr Syntax Error
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 Welcome
Expr Length Syntax Error
to LinuxQuestions.org, a friendly and active Linux Community. You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today! Note that expr index syntax error registered members see fewer ads, and ContentLink is completely disabled once you log in. Are you new to LinuxQuestions.org? Visit the following links: Site Howto | Site FAQ | Sitemap | Register Now If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here. Having a problem logging in? Please visit this page to clear all LQ-related cookies. Introduction to Linux - A Hands on Guide This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter. For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a bet
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this
Shell Script For Multiplication Of Two Numbers
site About Us Learn more about Stack Overflow the company Business Learn more shell script multiplication table about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x bash multiplication decimals Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up bash expr http://www.linuxquestions.org/questions/linux-general-1/how-to-multiply-with-expr-expr-3-*-4-doesn't-work-though-expr-3-4-does-631064/ syntax error when multiplying variable by integer up vote -1 down vote favorite I am attempting to learn bash, and I tried making a simple bash script to test my terminal. However, when using the expr command, the system keeps telling me that the syntax for the expr command is wrong. Here is the script: #!/bin/bash declare -a typo x=0 y=25 function arrayAndDisplay { http://stackoverflow.com/questions/27762913/bash-expr-syntax-error-when-multiplying-variable-by-integer for x in y; do typo[x]= `expr $x * 25` echo ${typo[x]} done } arrayAndDisplay When I try running the script in the terminal, this is the error message I get: expr: syntax error I've looked at reference websites and other StackOverflow posts, but nothing seems to work. Can anyone help me? P.S: I use Mac OS X Yosemite (10.10.1) bash osx-yosemite share|improve this question asked Jan 4 '15 at 6:08 DJ Head-On 69111 Remove space after = and what is the purpose of for x in y? –anubhava Jan 4 '15 at 6:11 @anubhava ok, but what do you suggest instead of for x in y? –DJ Head-On Jan 4 '15 at 6:13 Btw, typo[x]=$((x*25)) –Cyrus Jan 4 '15 at 6:16 1 Take a look at shellcheck.net –Cyrus Jan 4 '15 at 6:19 Are you trying to run a loop from 0 to 24? –anubhava Jan 4 '15 at 6:22 add a comment| 3 Answers 3 active oldest votes up vote 1 down vote The * in your expr is being expanded, so you're actually passing a list of file
Get Kubuntu Get Xubuntu Get Lubuntu Get UbuntuStudio Get Mythbuntu Get Edubuntu Get Ubuntu-GNOME Get UbuntuKylin Ubuntu Code of Conduct Ubuntu Wiki Community Wiki Other Support Launchpad Answers Ubuntu IRC Support AskUbuntu Official Documentation User Documentation Social Media Facebook Twitter Useful Links https://ubuntuforums.org/showthread.php?t=1564882 Distrowatch Bugs: Ubuntu PPAs: Ubuntu Web Upd8: Ubuntu OMG! Ubuntu Ubuntu Insights Planet Ubuntu Activity Page Please read before SSO login Advanced Search Forum The Ubuntu Forum Community Ubuntu Specialised Support Development & Programming Programming Talk [SOLVED] expr broken https://nixshell.wordpress.com/2007/01/29/simple-maths-in-the-unix-shell/ in Lucid? Having an Issue With Posting ? Do you want to help us debug the posting issues ? < is the place to report it, thanks ! Results 1 to 5 of 5 Thread: expr broken in Lucid? syntax error Thread Tools Show Printable Version Subscribe to this Thread… Display Linear Mode Switch to Hybrid Mode Switch to Threaded Mode August 31st, 2010 #1 Fatman_UK View Profile View Forum Posts Private Message Visit Homepage A Carafe of Ubuntu Join Date Dec 2007 Location ur LHC leakin ur protons Beans 87 DistroKubuntu 10.04 Lucid Lynx expr broken in Lucid? Hi all, Is expr broken? Code: $ expr 2 * 3 expr: syntax error $ expr 2*3 2*3 $ expr "2 expr syntax error * 3" 2 * 3 $ expr (2 * 3) bash: syntax error near unexpected token `2' $ expr \(2 * 3\) expr: syntax error $ expr "\(2 * 3\)" \(2 * 3\) $ expr \(2 * 3) bash: syntax error near unexpected token `)' The manpage suggests you can feed a multiplication to expr. Did I miss something? ps: I already found a workaround for this, which is to use "echo "2 * 3" | bc" instead. I'm just worried that expr is broken. Adv Reply August 31st, 2010 #2 schauerlich View Profile View Forum Posts Private Message Fresh Brewed Ubuntu Join Date Apr 2007 Location NorCal Beans 1,149 DistroUbuntu 10.04 Lucid Lynx Re: expr broken in Lucid? Code: schauerlich@inara:~$ expr 3 \* 5 15 * is auto-expanded by bash, you have to escape it. Posting code? Use the [code] or [php] tags. I don't care, I'm still free. You can't take the sky from me. Adv Reply August 31st, 2010 #3 amauk View Profile View Forum Posts Private Message Iced Almond Soy Ubuntu, No Foam Join Date Sep 2007 Location England Beans 1,103 Re: expr broken in Lucid? You can also do Code: expr 3 '*' 5 from expr man page Beware that many operators need to be escaped or quoted for shells. Adv Reply August 31st, 2010 #4 ghostdog74 View Profile View Forum Posts Private Message I Ubu
simple) maths. In most languages, x = x + 1 (or even x++) does exactly what you would expect. The Unix shell is different, however. It doesn't have any built-in mathematical operators for variables. It can do comparisons, but maths isn't supported, not even simple addition. Following the Unix tradition ("do one thing, and do it well") to the extreme, because the expr and bc utilites can do maths, there is absolutely no need for sh to re-invent the wheel. Yes, I agree. This is frustrating. If I've got one gripe against shell programming, then this is it. Addition and Subtraction So how do we cope? There are basically two ways, depending on whether we choose expr or bc: #!/bin/sh echo "Give me a number: " read x echo "Give me another number: " read y ###### Here's where we have the two options: # The expr method: exprans=`expr $x + $y` # The bc method: bcans=`echo $x + $y | bc` ###### Did you see the difference? echo "According to expr, $x + $y = $exprans" echo "According to bc, $x + $y = $bcans" As you can see, the language is slightly different for the two commands; expr parses an expression passed to it as arguments: expr something function something whereas bc takes the expression as its input (stdin): echo something function something | bc. Also, for expr, you must put spaces around the arguments: "expr 1+2" doesn't work. "expr 1 + 2" works. Multiplication Multiplication is a little awkward, too; the * asterisk, which traditionally denotes multiplication, is a special character to the shell; it means "every file in the current directory", so we have to delimit it with a backslash. "*" becomes "\*": #!/bin/sh echo "Give me a number: " read x echo "Give me another number: " read y ###### Here's where we have the two options: # The expr method: exprans=`expr $x \\* $y` # The bc method: bcans=`echo $x \\* $y | bc` ###### Did you see the difference? echo "According to expr, $x * $y = $exprans" ech