0

When I run the following command:

expr 1.2 * 13

I get this error:

expr: syntax error: unexpected argument '12'

I expect I am missing a syntax. What is it?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Allan Xu
  • 7,998
  • 11
  • 51
  • 122

1 Answers1

0

The asterisk is expanded as a glob, so you'll end-up running something like expr 1.2 file1 file2 13

Also, the shell cannot do arithmetic with floating point numbers floating point arithmetic with the shell isn’t standard:

expr 1.2 \* 13
expr: non-integer argument

You'll need to use a command like bc/awk/etc... or convert your numbers to integers:

echo '1.2 * 13' | bc
15.6

awk 'BEGIN{ print 1.2 * 13 }'
15.6

expr 12 \* 13 / 10
15
Fravadona
  • 13,917
  • 1
  • 23
  • 35
  • @ Fravadona : `"the shell cannot do arithmetic with floating point numbers"` ??? ::::::::::: `echo $(( 4.9 * 5.111 ))` ——> `25.043900000000001` :::::::::::: `expr $(( 2.7182818 ** 3.1415928 ))` ——> `23.140695259694844` – RARE Kpop Manifesto Dec 25 '22 at 10:31
  • @RAREKpopManifesto, ..."right", if you're okay with getting into the habit of writing code that's buggy on any standard-compliant shell. For someone who's a professional system administrator, that's dangerous. (Apple "chooses" to use an ancient version of bash because they're unwilling to be subject to GPLv3 license terms required for any build upstream shipped in over a decade, so the version of that they ship is exceptionally awful, with an ever-growing list of features it doesn't support; that's putting business concerns over customer wellbeing if I've ever seen it) – Charles Duffy Dec 25 '22 at 14:13