Wednesday, January 04, 2006

Functions Don't Return Values

Functions really don't return values. Really. That's hard to get used to. The shell does have a return() call, but it doesn't do what you'd guess.
#!/bin/bash

jo_mamma() {
return 69
}

fezmo=jo_mamma
echo fezmo is $fezmo

# No? Well, what if we reason by analogy and use $() to call the function?

fezmo=$(jo_mamma)
echo fezmo is $fezmo

# Still no. But look at this:

jo_mamma
echo after call, status is $?
return() returns from a function call and sets the predefined variable, $? -- the "exit status."

Even this isn't as useful as you'd hope. Substitute a string for the "69" in the example to see what happens.

0 Comments:

Post a Comment

<< Home