bash how to return from function

Answer

Use the statement "return" to return from function. You can also specify return value. Example:

return 1234

We returned function status 1234 from function. Usually we use 0 value for success and 1 for failure. It is similar to command exit which we use to terminate script.

If you don't use return statement in whole function, the status of the last executed command will be returned.

To verify returned status from the last called function use "$?".

There is difference between commands return and exit. Exit will cause the script end at the line where it is called. Return will cause the current function go out of scope and continue execution command after function.

Was this information helpful to you? You have the power to keep it alive.
Each donated € will be spent on running and expanding this page about UNIX Shell.