4

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
}
hjpotter92
  • 78,589
  • 36
  • 144
  • 183
Colas
  • 3,473
  • 4
  • 29
  • 68

2 Answers2

3

Replace return with echo and your code works fine.

Blender
  • 289,723
  • 53
  • 439
  • 496
  • Indeed, I remarked that but isn't it strange ? I would say that the role of `return` is to return a value, and the role of `echo` is to print a value. – Colas Sep 14 '12 at 08:42
  • 2
    Type in `man return` into your shell and read the manpage. `return` is for returning status codes. – Blender Sep 14 '12 at 08:45
  • So, the return of a function is with `echo` ? – Colas Sep 14 '12 at 08:53
  • More or less: http://stackoverflow.com/questions/3236871/how-to-return-a-string-value-from-a-bash-function – Blender Sep 14 '12 at 08:55
  • 5
    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 Sep 14 '12 at 09:59
  • 2
    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 Sep 14 '12 at 13:47
  • @chepner Functions absolutely do return a value. The value they return is assigned to `$?` and is used to determine if the function succeeded or failed. – William Pursell Sep 14 '12 at 16:06
  • I'm trying to draw a distinction between exit status and a value computed by the function. You wouldn't typically define an add function as `add () { return $(( $1 + $2 )); }`. – chepner Sep 14 '12 at 16:08
0

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

Colas
  • 3,473
  • 4
  • 29
  • 68