Reputation: 3573
In an inline shell, I type echo $(max 15 2)
but don't get any answer?
Why is it so?
Code:
function max {
if [ "$1" -eq "$2" ]
then
return $1
else
if [ "$1" -gt "$2" ]
then
return $1
else
return $2
fi
fi
}
Upvotes: 4
Views: 576
Reputation: 3573
From the comments :
Replace return with echo and your code works fine. - Blender
The $(...)
syntax is specifically designed to give you the output of a command, even if that command happens to be a function call. return in a function is similar to exit for the script as a whole; it sets its status, which is an integer in the range 0 to 255. (This is quite different from other languages you might be used to, where return is used to return a value from a function.) – Keith Thompson
Bash functions are not like functions in other languages. They behave the same as any other command: they can take command line arguments, read from standard input, write to standard output and standard error, and return with an exit status. They don't--strictly speaking--return a computed value. – chepner
Upvotes: 0