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 3493524
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T11:53:35+00:00 2026-05-18T11:53:35+00:00

This Bash snippet works as expected: $ fun1() { x=$(false); echo "exit code: $?";

  • 0

This Bash snippet works as expected:

$ fun1() { x=$(false); echo "exit code: $?"; }
$ fun1
exit code: 1

But this one, using local, does not as I would have expected:

$ fun2() { local x=$(false); echo "exit code: $?"; }
$ fun2
exit code: 0

Can anyone explain why does local discards the return code of the command?

  • 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-18T11:53:36+00:00Added an answer on May 18, 2026 at 11:53 am

    The reason the code with local returns 0 is because $? “Expands to the exit status of the most recently executed foreground pipeline.” Thus $? is returning the success of local

    You can fix this behavior by separating the declaration of x from the initialization of x like so:

    $ fun() { local x; x=$(false); echo "exit code: $?"; }; fun
    exit code: 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using GNU bash, version 3.00.15(1)-release (x86_64-redhat-linux-gnu). And this command: echo -e doesn't print
Consider the following code snippet: #!/bin/bash #This program tests the if statement in bash.
In bash one can escape arguments that contain whitespace. foo a string This also
NOTE: I thought I was using bash, but /bin/sh is linked to /bin/dash, which
I want to download a html file, by using this bash command system(wget -q
I want to write this bash loop for zsh while true; do echo print
I'm referring to distinctions such as in this answer : ...bash isn't for writing
Question: I have a question that is apparently not answered by this already-asked Bash
I have this line in a useful Bash script that I haven't managed to
When I commit I get this error from Subversion: bash-2.05b$ svn commit -m testing

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.