Skip to main content
civilfritz.net
  • Technology
    Posts Homelab CHANGELOG CU Research Computing CURCast (podcast)
  • Philosophy
    Posts Meditation Music
  • Gaming
    Posts Game Older Game Older (YouTube)
  • anderbubble
    Posts GitHub We The Players Grouvee OGS
  • Tags
  • Archive

    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 © 2022 Jonathon Anderson unless otherwise indicated - Powered by Nikola Creative Commons License