{
"$type": "site.standard.document",
"canonicalUrl": "https://rednafi.com/misc/return-values-from-a-shell-function/",
"description": "Understand how return values work in Bash functions. Learn exit codes, status evaluation patterns, and proper boolean returns with true/false commands.",
"path": "/misc/return-values-from-a-shell-function/",
"publishedAt": "2022-09-25T00:00:00.000Z",
"site": "at://did:plc:fgtm2c26vfcj74rfmeggbyqj/site.standard.publication/3mnl6f7ob462z",
"tags": [
"Shell",
"Unix",
"TIL"
],
"textContent": "TIL that returning a value from a function in bash doesn't do what I thought it does.\nWhenever you call a function that's returning some value, instead of giving you the value,\nBash sets the return value of the callee as the status code of the calling command. Consider\nthis example:\n\nI was expecting this to print out 42 but instead it doesn't print anything to the console.\nTurns out, a shell function doesn't return the value when it encounters the return\nkeyword. Rather, it stops the execution of the function and sets the status code of the last\ncommand in the function as the value that the function returns.\n\nTo test it out, you can print out the status code of the last command when a script exits\nwith echo $?. Here's the same snippet from the previous section where the last line is the\ncommand that calls the return_42 function:\n\nRun the snippet and print the exit code of the last line of the script with the following\ncommand:\n\nThis prints out:\n\nStatus code evaluation pattern\n\nHere's one pattern that you can use whenever you need to return a value from a shell\nfunction. In the following snippet, I'm evaluating whether a number provided by the user is\na prime or not and printing out a message accordingly:\n\nSince the returned values are treated as status codes where 0 is used to denote no error\nand a non-zero value represents an error, you'll need to return 0 as a truthy value and\n1 as a falsy value. While this works, returning 0 to denote a truthy value is the\nopposite of what you'd usually do in other programming languages and can confuse someone who\nmight not be familiar with shell quirks. If you only need to return a boolean value from a\nfunction, here's a better pattern:\n\nIn this snippet, notice how the is_prime function doesn't explicitly return anything.\nInstead, it just adds the true or false expression to the end of the return path\naccordingly. This implicitly sets the status code to 0 when the input number is a prime\nand to 1 when it's not. The rest of the status checking works the same as in the previous\nscript.\n\nThe second pattern won't work if you need to set the status code to something other than 0\nor 1. In that case you can resort the first pattern without confusing anyone.\n\nFurther reading\n\n- [Returning a boolean from a Bash function]\n\n\n\n\n[returning a boolean from a bash function]:\n https://stackoverflow.com/questions/5431909/returning-a-boolean-from-a-bash-function/43840545#43840545",
"title": "Returning values from a shell function"
}