0

I can get the return value from a function foo without problem such as:

$ cat test.sh
#!/usr/bin/bash
foo()
{
  echo "hello"
  return 20
}

bar()
{
    x=$(foo)
    rc=$?
    echo $rc
}
bar

$ test.sh
20

However, I cannot get the return value when I call function foo when assigning its output to a local variable.

$ cat test2.sh
#!/usr/bin/bash
foo()
{
  echo "hello"
  return 20
}

bar()
{
    local x=$(foo)
    local rc=$?
    echo $rc
}
bar

$ bash t2.sh
0

Is there a way to get around it without losing the local scope declaration?

5
  • 3
    Hi. You do not really need this forum. Check your script with shellcheck.net . Commented Jul 29 at 19:18
  • I’m voting to close this question because the poster didn't use shellcheck.net as directed by the bash tag. Commented Jul 30 at 10:21
  • That's not a valid close reason @EdMorton Lack of effort is a downvote reason, not a close reason. Commented Aug 1 at 18:05
  • @TylerH not running a script through shellcheck.net is not just lack of effort, it's ignoring the clear, simple instruction at the top of the bash tag they used "NOTE: Do not ask a bash question until you have copy/pasted your script into shellcheck.net and fixed all of the issues it tells you about.". Had the OP done that, shellcheck would have told them what's wrong with their script and how to fix it and then they wouldn't have had to ask this question. You are, of course, welcome to your own opinion on whether or not that's worth a close vote - in my opinion it is. Commented Aug 2 at 0:30
  • 1
    @EdMorton "didn't read a tag wiki" is also not a valid close reason. Tag wikis can be edited by anybody. This isn't a matter of opinion, it's a matter of not misusing a site feature. Please see stackoverflow.com/help/closed-questions and stackoverflow.com/help/privileges/close-questions to learn more about when you should cast a close vote on a question. Commented Aug 3 at 21:33

2 Answers 2

3

Make the variable local in one command, then assign to it as a separate command:

bar()
{
    local x
    x=$(foo)
    local rc=$?
    echo $rc
}

Since the assignment is separate from the local declaration, the commands' statuses don't conflict.

Sign up to request clarification or add additional context in comments.

Comments

2

If using bash 4.3+ I'd redesign the functions so that bar sends a variable name to foo and then foo populates said variable via a 'nameref'.

One coding approach:

foo()
{
  declare -n _input_var="$1"            # '-n' => define nameref; replace '_input_var' with any name you're
                                        # 100% sure won't be used by any other functions and/or scripts
  _input_var="hello"
  return 20
}

bar()
{
    local x 
    foo x                               # pass the name of the variable to foo()
    local rc=$?
    echo "$rc:$x:"
}

Taking for a test drive:

$ bar
20:hello:

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.