0

I am using notepad++ editor and executing the shell script in Cygwin terminal.

x=5
y=6
z=`expr x + y`
echo $z

Following error is seen:

expr: non-integer argument

What is wrong with the script?

ddmps
  • 4,350
  • 1
  • 19
  • 34
Ravi
  • 239
  • 2
  • 14
  • 1
    Aside from what varro correctly said: Why do you need `expr` here? Since you are using bash and integer arithmetic, a simple `((z=x+y))` would do. – user1934428 Jul 11 '18 at 06:20
  • Agreed, since I am new to shell, don't have the complete idea. So started with some basics. Thanks for the info – Ravi Jul 11 '18 at 08:16

1 Answers1

3

You have to dereference the variables:

z=`expr $x + $y`

Also, make sure your script has POSIX line endings (LF), not DOS-style (CRLF) line endings. (Use dos2unix or similar to convert.)

varro
  • 2,382
  • 2
  • 16
  • 24
  • It works fine in Linux machine. But in cygwin still I see same error - "expr: non-integer argument". Looks like notepad++ editor adds some unwanted things. With ((z=x+y)), I am getting error as- "syntax error near unexpected token `$'\r'" – Ravi Jul 11 '18 at 13:53
  • https://stackoverflow.com/questions/28965805/expr-non-integer-argument-while-doing-arithmetic-in-shell-script?rq=1 Found another link with same issue. Can someone help? – Ravi Jul 11 '18 at 14:11
  • 1
    The link you mention points you to the problem - you probably have CRLF (DOS-style) line endings in your script. You must convert the script to POSIX format, using, e.g., `dos2unix`. – varro Jul 11 '18 at 15:34
  • Issue solved: Got to Edit in notepad++, then EOL Conversion, then select UNIX/OSX Format. – Ravi Jul 13 '18 at 16:39