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

Recommended Answers

All 6 Replies

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.

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

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

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

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

You can also, as long as it is not csh or sh, just simply do
var=$(( 2 + 4 ));
var=$(( $var + 2 ));

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.