Linux.com | The source for Linux information
Published on Linux.com | The source for Linux information (https://www.linux.com)

Home > Returning Values from Bash Functions

Returning Values from Bash Functions [1]

Submitted by LinuxJournal [2] on September 11, 2009

Bash functions, unlike functions in most programming languages do not allow you to return a value to the caller. When a bash function ends its return value is its status: zero for success, non-zero for failure. To return values, you can set a global variable with the result, or use command substitution, or you can pass in the name of a variable to use as the result variable. The examples below describe these different mechanisms.

Although bash has a return statement, the only thing you can specify with it is the function's status, which is a numeric value like the value specified in an exit statement. The status value is stored in the $? variable. If a function does not contain a return statement, its status is set based on the status of the last statement executed in the function. To actually return arbitrary values to the caller you must use other mechanisms.

Article Source Linux Journal [3]
September 11, 2009, 11:30 am
Read More [4]

Tutorial Category: 
Tutorials [5]
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) })(window,document,'script','https://www.google-analytics.com/analytics.js','ga'); ga('create', 'UA-831873-8', 'auto'); ga('send', 'pageview'); -->

Source URL: https://www.linux.com/learn/returning-values-bash-functions

Links:
[1] https://www.linux.com/learn/returning-values-bash-functions
[2] https://www.linux.com/users/linuxjournal
[3] http://feedproxy.google.com/~r/linuxjournalcom/~3/qRfvsqQsJRU/return-values-bash-functions
[4] http://www.linuxjournal.com/content/return-values-bash-functions
[5] https://www.linux.com/tutorials/category/tutorials