LinuxQuestions.org
Review your favorite Linux distribution.
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-02-2011, 05:11 PM   #1
hd_pulse
Member
 
Registered: Dec 2010
Posts: 35

Rep: Reputation: 0
Confusion with assignment of expressions


Hello,

I have a big confusion in the assignment of expressions.

say i've to write an expression for a=a-1 (in C++) in shell scripting

so which to use and when

Quote:
a=$(expr $a - 1 )
OR

Quote:
((a=a-1))
OR

Quote:
a=' expr $a - 1 ' #single quote
OR

Quote:
a=`expr $a-1` # backstic is used
Of the 4 statements above whic is correct ?
Also explain how and when such expressions are precisely used?

Can the other operations like % + / * be also used with above expressions?
 
Old 06-02-2011, 07:14 PM   #2
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721
Use this:

Code:
((a = a - 1))
If you put a dollar sign in front of that, the result is printed to stdout. Without the dollar sign, nothing is printed and it returns success when the result is zero, and faiure for zero (this is useful for if statements, when you don't want to print anything, etc).

Also, single quotes are for quoting. Backticks are for command substitution. Note that Bash has a better alternative to backquotes:

Code:
$(command)
It can't be confused with single quotes, is easy to nest, and backslashes inside are interpreted normally.

Last edited by MTK358; 06-02-2011 at 07:19 PM.
 
Old 06-02-2011, 09:00 PM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,837

Rep: Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987
As MTK358 explained, all except #3 will work to set the variable to the final value of the expression, as they all use substitution of some sort. #3 will only set the variable to the literal string value.

Using expr is highly discouraged these days, since it's an external program and your shell has the same functions built-in to it.

$(..) and `..` are command substitutions. They will run the commands inside them, and print the result back to the line for further processing. $(..) is highly recommended over `..`.

$((..)) and ((..)) are the preferred ways to access bash's internal arithmetic ability. The first works like $(..), but only for math expressions. The second is for use when you don't need any output to be printed, such as in tests, or when incrementing a numeric variable.

There's also the internal let command, which is similar in use to expr.

This link will tell you all you need to know:
http://mywiki.wooledge.org/ArithmeticExpression

Last edited by David the H.; 06-03-2011 at 10:43 AM. Reason: fixed link
 
Old 06-03-2011, 02:36 AM   #4
hd_pulse
Member
 
Registered: Dec 2010
Posts: 35

Original Poster
Rep: Reputation: 0
Suppose I've mod of a number to caculate and pass the output to bc.

Quote:
value=`echo $va % 4 | bc`
Above command does'nt works. the result is supposed to be piped to bc
How to use the backstick here then?

(As backsticks are for command substitution.)

The below one works fine
Quote:
value=`expr $year % 4 `
How to solve the above with $() ?


Are backticks used in conjunction with expr not with echo ?
 
Old 06-03-2011, 09:25 AM   #5
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721
Backticks and $() work exactly the same.

And you should really use $() instead of backticks.

Last edited by MTK358; 06-03-2011 at 09:26 AM.
 
Old 06-03-2011, 10:06 AM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,577
Blog Entries: 31

Rep: Reputation: 1196Reputation: 1196Reputation: 1196Reputation: 1196Reputation: 1196Reputation: 1196Reputation: 1196Reputation: 1196Reputation: 1196
Quote:
Originally Posted by MTK358 View Post
Use this:

Code:
((a = a - 1))
or ((a--)) as demonstrated in this command prompt session
Code:
c@CW8:~$ a=2
c@CW8:~$ ((a--))
c@CW8:~$ echo $a
1
 
Old 06-03-2011, 11:01 AM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,837

Rep: Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987
Quote:
Originally Posted by hd_pulse View Post
Suppose I've mod of a number to caculate and pass the output to bc.
Code:
value=`echo $va % 4 | bc`
Above command does'nt works. the result is supposed to be piped to bc
How to use the backstick here then?
There's no reason it shouldn't work. I just tried it and had no problems. And as been pointed out, $() works exactly the same as backticks, and recommended.

Code:
$ va=5.3

$ value=`echo $va % 4 | bc`
$ echo $value
1.3

$ value=$(echo $va % 4 | bc)
$ echo $value
1.3
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] C array assignment smeezekitty Programming 6 05-04-2010 07:18 PM
would and could somebody help me with my homework assignment please... DarDevy Linux - Newbie 3 04-20-2009 02:43 PM
Help understanding a C assignment. slzckboy Programming 3 02-22-2007 12:46 PM
Assignment of IP-Address santosh_pn Linux - Software 4 06-16-2005 11:22 AM
need help with class assignment tenraek Linux - General 4 04-03-2003 12:31 AM

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

All times are GMT -5. The time now is 01:11 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration