User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Shell Scripting section within the Software Development category of DaniWeb, a massive community of 456,441 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,607 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Shell Scripting advertiser: Programming Forums
Views: 1135 | Replies: 6
Reply
Join Date: Nov 2007
Posts: 3
Reputation: sanjay goyan is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
sanjay goyan sanjay goyan is offline Offline
Newbie Poster

Question Problem in expr 2+4

  #1  
Nov 6th, 2007
Dear friends ,

file is test.sh
echo expr 2+4;


its give result 2+4 not 6 so plz tell how can i get the right result

sanjay
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,878
Reputation: Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold 
Rep Power: 13
Solved Threads: 193
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Problem in expr 2+4

  #2  
Nov 6th, 2007
What shell are you using. If it is bash or csh or somesuch, you need to use backquotes:

echo `expr 2+4`

Hope this helps.
Reply With Quote  
Join Date: Nov 2007
Posts: 3
Reputation: sanjay goyan is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
sanjay goyan sanjay goyan is offline Offline
Newbie Poster

Re: Problem in expr 2+4

  #3  
Nov 6th, 2007
Dear ,

I am using Bash shell and i did echo `expr 2+4`


but it give 2+4 not 6 ..

plz help me sir why its not working ???????????


sanjay
Reply With Quote  
Join Date: Nov 2007
Posts: 3
Reputation: sanjay goyan is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
sanjay goyan sanjay goyan is offline Offline
Newbie Poster

Solution Re: Problem in expr 2+4

  #4  
Nov 6th, 2007
Dear friends ,

i solved this problem by following ..........


echo `expr 2 + 4`

its give result 6 .we should give space between operator and operand


sanjay
Reply With Quote  
Join Date: Oct 2007
Posts: 302
Reputation: stilllearning has a spectacular aura about stilllearning has a spectacular aura about 
Rep Power: 3
Solved Threads: 37
stilllearning stilllearning is offline Offline
Posting Whiz

Re: Problem in expr 2+4

  #5  
Nov 22nd, 2007
With bash if you are typing on the command line you can just type

$ expr 2 + 4
6

If you are putting it in a file, you can do

#! /bin/bash

expr 2 + 4
var=`expr 2 + 4`
echo $var


and it will print:
6
6
Reply With Quote  
Join Date: Oct 2007
Posts: 306
Reputation: eggi is on a distinguished road 
Rep Power: 2
Solved Threads: 29
eggi eggi is offline Offline
Posting Whiz

Re: Problem in expr 2+4

  #6  
Nov 25th, 2007
If you're using a more advanced shell than posix or sh, you can use "let" also - just a suggestion

let var=2+4
echo $var

let works the opposite of expr, in so far as the spaces go - you shouldn't put any spaces between operators and operand

, Mike
Linux and Unix Tips, Tricks and Individual Advice - The Linux and Unix Menagerie!
------------------------------------------------------------------------
Having trouble passing cert exams? Check out How To Pass Any Computer Certification Test!
Reply With Quote  
Join Date: Feb 2006
Posts: 1,509
Reputation: masijade is a glorious beacon of light masijade is a glorious beacon of light masijade is a glorious beacon of light masijade is a glorious beacon of light masijade is a glorious beacon of light 
Rep Power: 10
Solved Threads: 136
masijade's Avatar
masijade masijade is offline Offline
Posting Virtuoso

Re: Problem in expr 2+4

  #7  
Nov 25th, 2007
You can also, as long as it is not csh or sh, just simply do
var=$(( 2 + 4 ));
var=$(( $var + 2 ));
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Shell Scripting Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Shell Scripting Forum

All times are GMT -4. The time now is 1:43 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC