Syntax. Bash function and exiting early 2019-10-18. These variables are visible only to the function and the commands it invokes. Every Linux or Unix command executed by the shell script or user, has an exit status. There is a simple, useful idiom to make your bash scripts more robust - ensuring they always perform necessary cleanup operations, even when something unexpected goes wrong. In using the first syntax, you have to use the keyword function, followed by your function name and open and close parentheses and curly braces to separate the contents of your functions to your main routine. and branches based on whether it is True (0) or False (not 0). That means, the original issue I sought out to fix wouldn’t actually be fixed. Although the tests above returned only 0 or 1 values, commands may return other values. Creating Functions. The bash supports two structures for functions. A non-zero (1-255) exit status … indeed that works in my test script (2 files, 1st an ordinary script that sources the 2nd) and test command (just sourcing the 2nd file) on my version of bash (4.4.20 on Ubuntu 18.04 LTS). You don’t put parentheses around the arguments like you might expect from some programming languages. More on Linux bash shell exit status codes. … If return is used outside a function, but during execution of a script by the . what you said was to put my code in a function. The exit status is an integer number. The bash if command is a compound command that tests the return value of a test or command ($?) return command is used to exit from a shell function. The secret sauce is a pseudo-signal provided by bash, called EXIT, that you can trap ; commands or functions trapped on it will execute when the script exits for any reason. Don’t … In computer a shell function name can take an input, $1 and return back the value (true or false) to the script. *This can often indicate a design problem. It takes a parameter [N], if N is mentioned then it returns [N] and if N is not mentioned then it returns the status of the last command executed within the function or script. is used to display the last return status. It turns out when you cal a Bash function using the syntax $() you are actually invoking a subshell (duh!) From man bash: return [n] Causes a function to stop executing and return the value specified by n to its caller. The return command causes a function to exit with the return value specified by N and syntax is: return N that's something very different. Variables local to the function may be declared with the local builtin. Syntax: return [N] Example: Note: echo $? Perhaps the most elegant solution is to keep a global name for the function return value and use it consistently in every function you write. N can only be a numeric value. which means exiting in the Bash function, only exits from that shell - which makes sense but I didn’t know that. #5 building As mentioned earlier, the "correct" way to return a string from a function is to replace it with a command. Option: return –help: It displays help information. * Set Retval + single return if the function is more complex and could have multiple exit points otherwise (readability issue). For the bash shell’s purposes, a command which exits with a zero (0) exit status has succeeded. Also, I'm pretty sure you don't want to return failure for the first line that doesn't match, just if no line matched: If n is omitted, the return status is that of the last command executed in the function body. They do not make your function return those values, to do that use the return command, and integer values corresponding to success (0) or failure (anything other than 0). true and false are commands that exit with success and failure exit codes, respectively. Put any parameters for a bash function right after the function’s name, separated by whitespace, just like you were invoking any shell script or command. In other words, you can return from a function with an exit status. Early return if there is some obvious dead end condition to check for right away that would make running the rest of the function pointless. If a numeric argument is given to return, that is the function’s return status; otherwise the function’s return status is the exit status of the last command executed before the return.