How do I assign x the value of x + 1? I can do it in other languages but can't figure it out in bash.
Tell me more
×
Super User is a question and answer site for
computer enthusiasts and power users. It's 100% free, no registration required.
|
I just tested two different ways and both worked for me:
...or...
|
|||
|
|
|
this should do the work
|
|||||||
|
|
This might work:
|
|||||||||||||||
|
|
@Bandit's answer is fine, but I want to highlight the difference that "let" and (( )) make to normal shell syntax:
causes bash (or ksh, or any POSIX shell) to treat the expression as an "arithmetic evaluation" in which the referenced variables don't need to be preceeded with "$". One advantage of using (( )) is that otherwise-special tokens don't need to be quoted or escaped, e.g. "*" for multiplication as in:
I find this syntax slightly clearer than $(( )) which uses the output of the expression, e.g.
|
|||
|
|