LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   brackets in bash shell (https://www.linuxquestions.org/questions/programming-9/brackets-in-bash-shell-815108/)

lipun4u 06-19-2010 08:04 AM

brackets in bash shell
 
Plz check the code...
Code:

#!/bin/sh

var1=100
var2=50
var3=45

var4=$[$var1 * ($var2 - $var3)]
echo "The final result is $var4"

Why it gives the following error ??
Quote:

/test101.sh: 7: Syntax error: "(" unexpected

jlinkels 06-19-2010 08:16 AM

Because this is not the correct syntax

http://www.faqs.org/docs/abs/HTML/arithexp.html

jlinkels

colucix 06-19-2010 08:19 AM

Maybe because you're running the code with /bin/sh and in compatibility mode with the old Bourne Shell the syntax is not correct. Anyway, the $[...] construct for integer expansion is deprecated and it should be avoided in favor of the arithmetic operator ((...)).

On the other hand, if you're really writing a script for the Bourne shell, better to use expr.

lipun4u 06-19-2010 08:24 AM

after changing to /bin/bash, everything ran correctly...


All times are GMT -5. The time now is 02:05 AM.