Why don't I return the function?

In the built-in shell, I print echo $(max 15 2) but don't get an answer?

Why is this so?

code:

 function max { if [ "$1" -eq "$2" ] then return $1 else if [ "$1" -gt "$2" ] then return $1 else return $2 fi fi } 
+4
source share
2 answers

From the comments:

  • Replace the return with an echo and your code works fine. - blender

  • The $(...) syntax is specifically designed to give you the output of a command, even if that command is a function call. return in the function is similar to the output for the script as a whole; it sets its status, which is an integer in the range from 0 to 255. (This is very different from other languages ​​that you might get 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 just like any other command: they can accept command line arguments, read from standard input, write to standard output and standard error, and return with exit status. They do not ... strictly speaking - return the calculated value. - chepner

0
source

Replace return with echo and your code will work fine.

+3
source

Source: https://habr.com/ru/post/1434147/


All Articles