Bash += and -=: Difference between revisions
Jump to navigation
Jump to search
(Created page with "=External= * https://linuxize.com/post/bash-increment-decrement-variable/ =Internal= * bash Concepts =Overview=") |
|||
(8 intermediate revisions by the same user not shown) | |||
Line 4: | Line 4: | ||
=Internal= | =Internal= | ||
* [[Bash_Concepts#.2B.3D|bash Concepts]] | * [[Bash_Concepts#.2B.3D|bash Concepts]] | ||
* [[((...))|((...))]] | |||
=Overview= | =Overview= | ||
+= and -= are assignment operators that can be used to increment/decrement the value of the left operand with the value specified after the operator. The operators can be used on integral values as such: | |||
<syntaxhighlight lang='bash'> | |||
i=0 | |||
((i+=1)) | |||
echo ${i} | |||
</syntaxhighlight> | |||
displays: | |||
<syntaxhighlight lang='bash'> | |||
1 | |||
</syntaxhighlight> | |||
Also: | |||
<syntaxhighlight lang='bash'> | |||
i=0 | |||
((i-=1)) | |||
echo ${i} | |||
</syntaxhighlight> | |||
displays: | |||
<syntaxhighlight lang='bash'> | |||
-1 | |||
</syntaxhighlight> | |||
+= can be used on string values as such: | |||
<syntaxhighlight lang='bash'> | |||
s="something" | |||
s+=" else" | |||
echo ${s} | |||
</syntaxhighlight> | |||
displays: | |||
<syntaxhighlight lang='text'> | |||
something else | |||
</syntaxhighlight> | |||
-= does not work with strings. |
Latest revision as of 05:39, 8 May 2020
External
Internal
Overview
+= and -= are assignment operators that can be used to increment/decrement the value of the left operand with the value specified after the operator. The operators can be used on integral values as such:
i=0
((i+=1))
echo ${i}
displays:
1
Also:
i=0
((i-=1))
echo ${i}
displays:
-1
+= can be used on string values as such:
s="something"
s+=" else"
echo ${s}
displays:
something else
-= does not work with strings.