It's possible to do something that looks similar to what you have shown, by using declare -i
:
declare -i nextcount
nextcount=$count+1
That declares the variable nextcount
with the integer attribute, which causes arithmetic evaluation to be performed on it automatically whenever it is assigned. (Run help declare
in bash or look here for details.) This is not a standard POSIX shell feature, but bash supports it.
The assignment does not need to immediately follow the declare
command. If it does, you may prefer to do it in the declare
command, like this:
declare -i nextcount=$count+1
Any arithmetic expression with integers and arithmetic operators is supported. If the operators risk being interpreted specially by the shell in another way, you can quote the expression to prevent that. Remember, you're assigning text to the variable; arithmetic evaluation is performed on this text. For another example, after this command $x
will expand to 13
:
declare -i x='2 * 7 - 1'
Even if you removed the spaces, you would want that quoted because *
would otherwise cause it to be interpreted as a glob, triggering filename expansion. Note that you must still not have spaces around =
.
You might not usually do it this way, though. Often it is easier, more elegant, and more readable to use arithmetic expansion (with $
) or arithmetic evaluation (without $
) instead, as the other answers suggest. I present the method shown here not to encourage you to prefer it, but instead mainly because it is the closest thing to what your book showed that actually performs shell arithmetic.
Arithmetic expressions--by which I mean expressions that are evaluated using shell arithmetic because they are inside $((
))
or ((
))
--permit you to use =
with spaces around it. The =
in the command ((count = 10))
does not work the same, syntactically, as the =
in the command count=10
.
If x
has the integer attribute (declare -i x
), you can even write things like this to assign a value to multiple variables, though I would suggest against it on the grounds that it is confusing:
x=y=z=10
If you're wondering what that does, here's a hint--this does the same thing:
x='y = z = 10'
That assigns the string y = z = 10
(or, in the first example, y=z=10
) to x
. It is important that at least x
have the integer attribute. Assuming it does, that text is evaluated as an arithmetic expression. The subexpression z = 10
assigns 10
to z
, but that subexpression also has a value, 10
, which is assigned to y
. That full expression, which is an assignment to y
, also has a value, still 10
.
Note that the first =
is doing normal shell variable assignment, while the others are the arithmetic assignment operator. This is not special to the second form (the one that, by being quoted, is able to contain spaces). In x=y=z=10
, too, the first =
means something different from the other two =
es, though you happen to be using them to achieve the same goal. This is potentially confusing. I recommend against writing stuff like that, except perhaps interactively for purposes of experimentation. You can write this command instead:
((x = y = z = 10))
That's straightforward arithmetic evaluation. All three =
signs are the arithmetic assignment operator. It's still fine if you have declared some or all of the variables with the integer attribute--which I often advocate--but you do not need to have done so.
In a sort of similar vein, bash has a +=
operator that normally appends text:
$ x=foo
$ x+=bar
$ echo "$x"
foobar
(The leading $
characters on some lines are my prompt. I include them here to distinguish the input I am giving bash from the output it produces.)
But the attributes of the assigned-to variable affect what operation +=
performs. +=
is commonly used to extend indexed arrays:
$ a=(foo bar baz)
$ a+=(quux)
$ echo "${a[@]}"
foo bar baz quux
When a variable with the integer attribute is assigned to with +=
, in-place addition is performed:
$ declare -i y=7
$ y+=2
$ echo "$y"
9
But bash also has +=
as an arithmetic operator, which I suggest using instead in most cases. That is, if y
holds the value 2
, you can write this instead to increase the value of y
by 2:
((y += 2))
It also has the other traditional compound assignment operators (the ones that are present in the C language). For example, suppose I want to decrease the value of y
by 3. This doesn't work:
$ y-=3
y-=3: command not found
But this works:
$ ((y -= 3))
$ echo "$y"
6
Or I could have just done this, which both changes y
and prints the new value:
$ echo "$((y -= 3))"
6
(( ... ))
to do arithmetics inbash
. – nohillside Apr 06 '18 at 12:51