How to increment local variable in Bash?
62,833
Solution 1
I'm getting 2
from your code. Nevertheless, you can use the same technique for any variable or number:
local start=1
(( start++ ))
or
(( ++start ))
or
(( start += 1 ))
or
(( start = start + 1 ))
or just
local start=1
echo $(( start + 1 ))
etc.
Solution 2
Try:
START2=$(( `getStart` + 1 ));
The $(( ))
tells bash that it is to perform an arithmetic operation, while the backticks tells bash to evaluate the containing expression, be it an user-defined function or a call to an external program, and return the contents of stdout.
Related videos on Youtube
![Léo Léopold Hertz 준영](https://i.stack.imgur.com/0VZ1V.png?s=256&g=1)
Author by
Léo Léopold Hertz 준영
Updated on September 18, 2022Comments
-
Léo Léopold Hertz 준영 almost 2 years
Data
1 \begin{document} 3
Code
#!/bin/bash function getStart { local START="$(awk '/begin\{document\}/{ print NR; exit }' data.tex)" echo $START } START2=$(getStart) echo $START2
which returns
2
but I want3
. I change unsuccessfully the end by this answer about How can I add numbers in a bash script:START2=$((getStart+1))
How can you increment a local variable in Bash script?
-
Bruno Bieri over 7 yearsThis may also help: askubuntu.com/questions/385528/…