Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 want 3. 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?

share|improve this question
    
I'm getting 2, not 1, from the code. – choroba Sep 11 '15 at 13:03
    
Sorry my mistake! – Masi Sep 11 '15 at 13:13
1  
OFF: why awk? sed -n '/begin{document}/{=;q}' data.text much shorter… – Costas Sep 11 '15 at 13:46
    
@Costas Yes, you are right! I have had today a bad day in thinking too complicated. Thinking now the thing here for open intervals: unix.stackexchange.com/q/229060/16920 Can you explain }/{=;q} this in an answer/comment, please? – Masi Sep 11 '15 at 13:47
up vote 7 down vote accepted

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.

share|improve this answer
    
This may also help: askubuntu.com/questions/385528/… – Bruno Bieri Oct 12 at 6:30

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.