1

This has been already discussed, but I have a more different problem: I have a function that needs to be called with $@ as parameter.

If I put var=$(function $@) I just receive errors for every line where the function actions.

Meanwhile I used a workaroud:

  1. I called the function first: function $@
  2. Then I stored into the variable the result from the function: var=$?

But this works just if the function return is "succes" or "fail". Any thoughts?

Code:

function()
{
    if [ $1 -gt $x ]
    then
        return 0
    fi

    if [ $1 -eq $x ]
    then
        return 1
    fi

    if [ $1 -lt $x ]
    then
        return 2
    fi
}

I want to store in my variable 0 , 1 or 2. For this:

menu ()
{
    if [ $# -gt 5 ] || [ $# -lt 1 ]
    then
        echo "Error! Script is: " $0
        return
    fi

    echo "Insert reference number: "
    read x

    while [ $# -gt 0 ]
    do
        rez=$(function $@)

        if [ $rez -eq 0 ]
        then
            echo "Nr >!" $1
        fi

        if [ $rez -eq 1 ]
        then
            echo "Nr =!" $1
        fi

        if [ $rez -eq 2 ]
        then
            echo "Nr <!" $1
        fi
        shift
    done
}
5
  • Please show some actual code and exact (verbatim) output/errors. Commented Mar 20, 2016 at 10:29
  • A bush function cannot return anything except success or failure. m.linuxjournal.com/content/return-values-bash-functions Commented Mar 20, 2016 at 10:30
  • function is a bash builtin keyword. I suggest to peplace both "function" by "foobar". Commented Mar 20, 2016 at 10:41
  • Yes, sorry. I just replaced my functions name (that was not in English) in a common name, for not creating any confusions. Commented Mar 20, 2016 at 10:44
  • Does this answer your question? How do I set a variable to the output of a command in Bash? Commented Jul 4, 2023 at 21:08

1 Answer 1

2
  1. Maybe use elifs so that you don't receive multiple values that are returned (also a case statement might be a better solution).

  2. var=$(function $@ >/dev/null 2>&1; echo $?) should do what you want, I believe?

Sign up to request clarification or add additional context in comments.

4 Comments

My task says so: return 0 for ... 1 for ... and 2 for .... I just need those returns. What exactly is that code line doing, can u please explain? It works just as I want, indeed.
It's executing your function while redirecting both stdout and stderr to /dev/null, ensuring there's no output from your function unintentionally stored in the variable. It then echoes the return value, which gets saved in the variable.
If the functions return somehow an error, will it still be visible if I use /dev/null?
No; if you want to keep some verbosity (but still only record the return value in the variable), you could use: var=$(function $@ >&2; echo $?) instead, which would redirect all output except the return value to stderr.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.