When I run commands in my shell as below, it returns an expr: non-integer argument
error. Can someone please explain this to me?
$ x=20
$ y=5
$ expr x / y
expr: non-integer argument
When I run commands in my shell as below, it returns an expr: non-integer argument
error. Can someone please explain this to me?
$ x=20
$ y=5
$ expr x / y
expr: non-integer argument
You can use command substitution as follows:
Make sure spaces as mentioned above
Referencing Bash Variables Requires Parameter Expansion
The default shell on most Linux distributions is Bash. In Bash, variables must use a dollar sign prefix for parameter expansion. For example:
Of course, Bash also has arithmetic operators and a special arithmetic expansion syntax, so there's no need to invoke the expr binary as a separate process. You can let the shell do all the work like this:
let's suppose
then
this will work properly . But if you want to use / operator in case statements than it can't resolve it. In that case use simple strings like div or devide or something else. See the code
I believe it was already mentioned in other threads:
then you can simply type :
In your case it will be:
or if you prefer, add this as a separate script and make it available in $PATH so you will always have it in your local shell:
Those variables are shell variables. To expand them as parameters to another program (ie
expr
), you need to use the$
prefix:The reason it complained is because it thought you were trying to operate on alphabetic characters (ie non-integer)
If you are using the Bash shell, you can achieve the same result using expression syntax:
Or:
Why not use let; I find it much easier. Here's an example you may find useful: