LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-19-2010, 08:04 AM   #1
lipun4u
Member
 
Registered: Sep 2008
Location: Mumbai, india
Distribution: ubuntu and hp-unix
Posts: 118

Rep: Reputation: 15
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
 
Old 06-19-2010, 08:16 AM   #2
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
Because this is not the correct syntax

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

jlinkels
 
Old 06-19-2010, 08:19 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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.
 
1 members found this post helpful.
Old 06-19-2010, 08:24 AM   #4
lipun4u
Member
 
Registered: Sep 2008
Location: Mumbai, india
Distribution: ubuntu and hp-unix
Posts: 118

Original Poster
Rep: Reputation: 15
after changing to /bin/bash, everything ran correctly...
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
A question about shell (bash shell) thomas2004ch Linux - Newbie 4 08-27-2009 02:09 AM
in bash shell how to run shell script during startup rammohan04 Red Hat 2 07-31-2009 02:07 AM
Two square brackets in bash condition tirengarfio Programming 1 07-07-2009 12:36 PM
Shell script:- Reading numbers embedded in brackets from a text file rsan Linux - Newbie 6 07-05-2009 06:01 AM
copy directories and files recursively using C shell or bash shell bostonuser Programming 7 06-06-2008 01:24 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:09 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration