Skip to main content
civilfritz.net
  • Posts
    All Posts Post Titles Tags
  • Shows
    CURCast Game Older
  • Music
    P.O.A.
  • Elsewhere
    GitHub Grouvee LinkedIn OGS YouTube

    bash ‘local’ directive eats status codes

    Jonathon Anderson

    2011-08-02 08:42

    Did you know that the local directive in bash returns a status code? I didn’t.

    #!/bin/bash
    
    function return_nonzero
    {
        return 1
    }
    
    function main
    {
        v=$(return_nonzero)
        echo $?
    
        local v=$(return_nonzero)
        echo $?
    
        local v
        v=$(return_nonzero)
        echo $?
    }
    
    main

    At runtime:

    $ bash local-return-code.sh
    1
    0
    1

    At least some bugs teach you something new.

    • technology
    • Previous post
    • Next post
    Contents © 2026 Jonathon Anderson unless otherwise indicated - Powered by Nikola Creative Commons License