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

I studied this article called Returning Values from Bash Functions. Data

Lorem.
\begin{document}
hello
\end{document}

Case #1 which does not work

Code

#!/bin/bash
function getStart {
        local START="$(awk '/begin\{document\}/{ print NR; exit }' data.tex)"
}

START2=$(getStart)
echo $START2

which returns falsely an empty line. I expect 1. Why does the script return an empty line?

Case #2 which works

Code

#!/bin/bash
function getStart {
        local START="$(awk '/begin\{document\}/{ print NR; exit }' data.tex)"
        echo $START
}

getStart

which prints correctly 1.

Output of choroba's answer

#!/bin/bash
function getStart {
        local START="$(awk '/begin\{document\}/{ print NR; exit }' data.tex)"
        echo $START
}

START2=$(getStart)
echo $START2

gives the linenumber only once, which is not the expected result. I think it should do it twice.

share|improve this question
    
Regarding your update, see my edit to the answer, but in general, don't expect people to answer updates to questions. Comment on the answer if you think it isn't clear, or ask a new question. – Gilles Sep 11 '15 at 22:57
    
The whole point of command substitution is that the outputs doesn't go out, but gets captured in a variable instead. – PSkocik Sep 11 '15 at 23:08
up vote 2 down vote accepted

$(...) (aka "Command substitution") captures the output of the ... command. Assigning a value to a variable produces no output, so there's nothing to capture. In case #2, echo produces the output.

getStart () {
    local l=Hallo
    echo $l
}

v=$(getStart)
echo $v

To answer your update: the function outputs Hallo. This output is captured by the command substitution, because that's what command substitution does, so up to v=$(getStart), the script produces no output. Then the line echo $v outputs Hallo.

share|improve this answer
    
Can you given an example how you would get the expected result with the case #1? – Masi Sep 11 '15 at 12:27
    
@Masi: You have to output the value, otherwise it gets lost when the function is left. – choroba Sep 11 '15 at 12:29
3  
@Masi: function is optional. The output of the first echo is consumed by the $(...), so you see only the second one. – choroba Sep 11 '15 at 12:35

Just run the awk command in it and forget about capturing. Functions can echo something and additionally, they do return an exit status. If you don't explicitly return (e.g., return 1), the exit status of a function will be the exit status of the last command. That's why you probably don't want to capture + echo in the function. It would shadow the exit status of the captured command with the exit status of echo (0, barring unusual circumstances).

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.