LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware
User Name
Password
Slackware This Forum is for the discussion of Slackware Linux.

Notices


Reply
  Search this Thread
Old 11-02-2013, 04:08 AM   #1
waddles
Member
 
Registered: Sep 2012
Posts: 372

Rep: Reputation: 1
Uncertain about command


I am wondering about the shell commands (( istop++ )) and
((shf=shf+bear)) in some functioning code. I am lead to believe it is equivalent to using expr. I would like to see some definition and could not find it in bash man page or elsewhere.
 
Old 11-02-2013, 04:28 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Quote:
Originally Posted by waddles View Post
I am wondering about the shell commands (( istop++ )) and
((shf=shf+bear)) in some functioning code. I am lead to believe it is equivalent to using expr. I would like to see some definition and could not find it in bash man page or elsewhere.
If you search for bash arithmetic you get a ton of hits.

Here are 3:
- Arithmetic Expansion (ABSG)
- Arithmetic in Bash
- Perform arithmetic operations
 
1 members found this post helpful.
Old 11-02-2013, 11:18 AM   #3
Alien Bob
Slackware Contributor
 
Registered: Sep 2005
Location: Eindhoven, The Netherlands
Distribution: Slackware
Posts: 8,559

Rep: Reputation: 8119Reputation: 8119Reputation: 8119Reputation: 8119Reputation: 8119Reputation: 8119Reputation: 8119Reputation: 8119Reputation: 8119Reputation: 8119Reputation: 8119
Quote:
Originally Posted by waddles View Post
I am wondering about the shell commands (( istop++ )) and
((shf=shf+bear)) in some functioning code. I am lead to believe it is equivalent to using expr. I would like to see some definition and could not find it in bash man page or elsewhere.
OK, let's just be lazy and steal directly from "man bash":

Code:
 ((expression))
              The  expression  is evaluated according to the rules described below under ARITHMETIC
              EVALUATION.  If the value of the expression is non-zero, the return status is 0; oth-
              erwise the return status is 1.  This is exactly equivalent to let "expression".
and

Code:
ARITHMETIC EVALUATION
       The  shell  allows  arithmetic expressions to be evaluated, under certain circumstances (see
       the let and declare builtin commands and  Arithmetic  Expansion).   Evaluation  is  done  in
       fixed-width integers with no check for overflow, though division by 0 is trapped and flagged
       as an error.  The operators and their precedence, associativity, and values are the same  as
       in  the  C language.  The following list of operators is grouped into levels of equal-prece-
       dence operators.  The levels are listed in order of decreasing precedence.

       id++ id--
              variable post-increment and post-decrement
       ++id --id
              variable pre-increment and pre-decrement
       - +    unary minus and plus
       ! ~    logical and bitwise negation
       **     exponentiation
       * / %  multiplication, division, remainder
       + -    addition, subtraction
       << >>  left and right bitwise shifts
       <= >= < >
              comparison
       == !=  equality and inequality
       &      bitwise AND
       ^      bitwise exclusive OR
       |      bitwise OR
       &&     logical AND
       ||     logical OR
       expr?expr:expr
              conditional operator
       = *= /= %= += -= <<= >>= &= ^= |=
              assignment
       expr1 , expr2
              comma

       Shell variables are allowed as operands; parameter expansion is performed before the expres-
       sion  is  evaluated.   Within  an expression, shell variables may also be referenced by name
       without using the parameter expansion syntax.  A shell variable that is null or unset evalu-
       ates  to  0 when referenced by name without using the parameter expansion syntax.  The value
       of a variable is evaluated as an arithmetic expression when it  is  referenced,  or  when  a
       variable which has been given the integer attribute using declare -i is assigned a value.  A
       null value evaluates to 0.  A shell variable need not have its integer attribute  turned  on
       to be used in an expression.

       Constants  with  a  leading  0 are interpreted as octal numbers.  A leading 0x or 0X denotes
       hexadecimal.  Otherwise, numbers take the form [base#]n, where the optional base is a  deci-
       mal  number  between  2  and  64 representing the arithmetic base, and n is a number in that
       base.  If base# is omitted, then base 10 is used.  The digits greater than 9 are represented
       by  the  lowercase letters, the uppercase letters, @, and _, in that order.  If base is less
       than or equal to 36, lowercase and uppercase letters may be used interchangeably  to  repre-
       sent numbers between 10 and 35.

       Operators  are  evaluated in order of precedence.  Sub-expressions in parentheses are evalu-
       ated first and may override the precedence rules above.
Eric
 
1 members found this post helpful.
Old 11-02-2013, 11:45 AM   #4
GazL
LQ Veteran
 
Registered: May 2008
Posts: 7,099

Rep: Reputation: 5262Reputation: 5262Reputation: 5262Reputation: 5262Reputation: 5262Reputation: 5262Reputation: 5262Reputation: 5262Reputation: 5262Reputation: 5262Reputation: 5262
If you're planning on using the return code for anything then watch out for the subtleties of those pre/post increment operators:
Code:
test@rc3:~$ stop=0
test@rc3:~$ (( stop++ )) && echo "Stop is $stop."
test@rc3:~$ (( stop++ )) && echo "Stop is $stop."
Stop is 2.
test@rc3:~$ stop=0
test@rc3:~$ (( ++stop )) && echo "Stop is $stop."
Stop is 1.
"(( stop += 1 ))" is generally a safer option if you're not used to dealing with them.
Code:
test@rc3:~$ stop=0
test@rc3:~$ (( stop += 1 )) && echo "Stop is $stop."
Stop is 1.
test@rc3:~$
 
1 members found this post helpful.
  


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
LXer: The Uncertain Age of Steam on Linux LXer Syndicated Linux News 0 02-01-2013 03:30 PM
LXer: Some Fedora 18 Features Are Still Uncertain LXer Syndicated Linux News 0 09-06-2012 08:10 AM
LXer: OpenSuse's uncertain future LXer Syndicated Linux News 0 11-26-2010 06:01 PM
LXer: Uncertain future for Flash LXer Syndicated Linux News 0 06-25-2010 07:31 PM
Uncertain about line code anaigini45 Linux - Newbie 1 12-24-2009 02:19 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware

All times are GMT -5. The time now is 10:14 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