Skip to content

Instantly share code, notes, and snippets.

@domanchi
Created November 3, 2017 20:08
Show Gist options
  • Save domanchi/14251dffe9b114e8427a7baff0659a54 to your computer and use it in GitHub Desktop.
Save domanchi/14251dffe9b114e8427a7baff0659a54 to your computer and use it in GitHub Desktop.
[bash return values] The better way to break bash scripts into functions. #bash

Bash Functions Return Values

The lame, old way: passing in a global variable, setting it in the function, then unsetting it later on.

#!/bin/bash

function foobar() {
    eval $1="some return value"
}

function main() {
    foobar retval
    local var=$retval
    unset $retval
}

But now, the new way!

#!/bin/bash

function foobar() {
    echo "some return value"
}

function main() {
    local var=$(foobar)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment