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
}
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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 ThompsonBash 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