Slackware This Forum is for the discussion of Slackware Linux.
|
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
11-02-2013, 04:08 AM
|
#1
|
Member
Registered: Sep 2012
Posts: 372
Rep:
|
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.
|
|
|
11-02-2013, 04:28 AM
|
#2
|
LQ Veteran
Registered: Sep 2003
Posts: 10,532
|
Quote:
Originally Posted by waddles
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.
|
11-02-2013, 11:18 AM
|
#3
|
Slackware Contributor
Registered: Sep 2005
Location: Eindhoven, The Netherlands
Distribution: Slackware
Posts: 8,559
|
Quote:
Originally Posted by waddles
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.
|
11-02-2013, 11:45 AM
|
#4
|
LQ Veteran
Registered: May 2008
Posts: 7,099
|
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.
|
All times are GMT -5. The time now is 10:14 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|