[bash] Assign function result to var without suppressing it's stdout

I don’t think i can go simpler than that :upside_down_face:

Check this out, if you diff that with OP and run both side by side - you’ll get what i mean:

#!/bin/bash
title=$(basename "$0")
echo -n -e "\033]0;$title\007"

# Force output in english
export LC_ALL=en_US.UTF-8


readarray some_data << EOF
0 1.7.17 We
1 2.11 Are
2 3.1 Really
3 1.7.31 Doomed
EOF


main() {

    echo
    echo "   --------------------   Select   --------------------   "
    echo

    CLI "list" some_data
    local result="${CLI_RESULT}"

    [[ "${result}" != "" ]] && echo "${result}" || echo "Nothing selected"
}

CLI() {

    unset CLI_RESULT
    local mode=$1
    local -n args="$2"

    case "$mode" in
        list)
            local length=${#args[@]}
            for (( i=0; i < ${length}; i++ ));
            do
                echo "${args[i]}"
            done | column -t -s " " -R "1"
            echo

            local input
            while true;
            do
                [ -n "$input" ] || read -r -p "-> " input

                if [[ "${input}" =~ ^[0-9]+$ ]] && [ "${input}" -lt "${length}" ];
                then
                    local choice="${args[${input}]}"
                    CLI_RESULT=$(echo "${choice}" | cut -d " " -f1)
                    break
                else
                    local input=""
                    echo "Wrong option"
                fi
            done
        ;;
    esac
}

main "$@"

This is pretty much exactly how i wanted it to work actually…But i thought maybe there is some more elegant method of doing so, than using global variable CLI_RESULT?

Would be cool to be able to get exactly same output as in that post, but with syntax of just assigning to variable:

local result=$(CLI "list" some_data)

Not sure if it’s possible at all


Very likely, i’m just not sure exactly how in that case