Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7010575
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:00:34+00:00 2026-05-27T22:00:34+00:00

Say I have the following two Bash scripts: Version #1: #!/bin/bash function bar {

  • 0

Say I have the following two Bash scripts:

Version #1:

#!/bin/bash

function bar
{
  if true; then
    echo "error" >&2
    exit 1
  fi
  echo "bar"
}

function foo
{
  local val=`bar`
  echo $?
  echo "val: $val"
}

foo

With version #2 second having a slightly different foo:

function foo
{
  val=`bar` #note no 'local'
  echo $?
  echo "val: $val"
}

Version #1 gives me the following output:

error
0
val:

Whilst version #2 gives me this:

error
1
val:

The inclusion of local in #2 appears to hide the return value of bar.

Am I correct in thinking this is because local is itself a function, and is returning 0? And if so, is there a way around this and make val a local variable, but still test the return value of bar?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T22:00:34+00:00Added an answer on May 27, 2026 at 10:00 pm

    Yes, you are reading the return value of local which was successful. The fix is to separate the variable declaration from its definition like so:

    #!/bin/bash
    
    function bar
    {
      if true; then
        echo "error" >&2
        exit 1
      fi
      echo "bar"
    }
    
    function foo
    {
      local val
      val=$(bar)
      echo $?
      echo "val: $val"
    }
    
    foo
    

    Output

    $ ./localtest
    error
    1
    val:
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have the following code: function One() {} One.prototype.x = undefined; function Two()
Say for example, I have the following two tables: TableA { _id } TableB
If I have two domain names: altcognito.com and say I've got the other following
Say I have the following web.config: <?xml version=1.0 encoding=utf-8?> <configuration> <system.web> <authentication mode=Windows></authentication> </system.web>
Say i have the following two classes: public class User { public int ID
Let's say I have any array Trial() As Integer I have two following variables
Let's say I have following Spring config (version of Spring is 3.0.3): <?xml version=1.0
Say I have the following string lists: string[] list1 = { one, two, three,
Say I have the following two functions: 1 int * foo() { int b=8;
Let's say you have the following two tables: X Table X_ID Y_ID_F X_Value 1

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.