#] #] ********************* #] "$d_SysMaint"'Linux/function notes.txt' # www.BillHowell.ca 2Aug2023 initial # view in text editor, using constant-width font (eg courier), tabWidth = 3 #48************************************************48 #24************************24 # Table of Contents, generate with : # $ grep "^#]" "$d_SysMaint"'Linux/function notes.txt' | sed "s/^#\]/ /" # #24************************24 #] Setup, ToDos, #] 02Aug2023 function returns via multiple echos https://stackoverflow.com/questions/17336915/return-value-in-a-bash-function What's annoying is that a function that should provide data cannot also echo other stuff to stdout, because the caller using $() will receive that too and get confused or have to parse the output. Global variables are not great because it's just a matter of time before you use the same global var in two places that happen to be nested and data could get lost. There should be separate channels for printing data vs sending data back. – Oliver Aug 2, >> I can use this for structured data returns #] 02Aug2023 Dynamic scoping means that ret_val points to a different object, #] depending on the caller! This is different from lexical scoping, #] which is what most programming languages use. #] +-----+ #24************************24 #08********08 #] ??Aug2023 #08********08 #] ??Aug2023 #08********08 #] ??Aug2023 #08********08 #] ??Aug2023 #08********08 #] ??Aug2023 #08********08 #] ??Aug2023 #08********08 #] 02Aug2023 search "Linux function returns and echo" +-----+ https://stackoverflow.com/questions/17336915/return-value-in-a-bash-function Return value in a Bash function Asked 10 years, 1 month ago Modified 1 year, 2 months ago Viewed 691k times I am working with a bash script and I want to execute a function to print a return value: function fun1(){ return 34 } function fun2(){ local res=$(fun1) echo $res } When I execute fun2, it does not print "34". Why is this the case? bashfunctionreturn-value edited Apr 11, 2018 at 21:45 learningbee asked Jun 27, 2013 at 7:17 mindia +--+ Although Bash has a return statement, the only thing you can specify with it is the function's own exit status (a value between 0 and 255, 0 meaning "success"). So return is not what you want. You might want to convert your return statement to an echo statement - that way your function output could be captured using $() braces, which seems to be exactly what you want. ... edited Mar 16, 2021 at 16:08 Peter Mortensen answered Jun 27, 2013 at 7:19 tamasgal +--+ What's annoying is that a function that should provide data cannot also echo other stuff to stdout, because the caller using $() will receive that too and get confused or have to parse the output. Global variables are not great because it's just a matter of time before you use the same global var in two places that happen to be nested and data could get lost. There should be separate channels for printing data vs sending data back. – Oliver Aug 2, +--+ The problem with other answers is they either use a global, which can be overwritten when several functions are in a call chain, or echo which means your function cannot output diagnostic information (you will forget your function does this and the "result", i.e. return value, will contain more information than your caller expects, leading to weird bugs), or eval which is way too heavy and hacky. The proper way to do this is to put the top level stuff in a function and use a local with Bash's dynamic scoping rule. Example: func1() { ret_val=hi } func2() { ret_val=bye } func3() { local ret_val=nothing echo $ret_val func1 echo $ret_val func2 echo $ret_val } func3 This outputs nothing hi bye Dynamic scoping means that ret_val points to a different object, depending on the caller! This is different from lexical scoping, which is what most programming languages use. This is actually a documented feature, just easy to miss, and not very well explained. Here is the documentation for it (emphasis is mine): Variables local to the function may be declared with the local builtin. These variables are visible only to the function and the commands it invokes. For someone with a C, C++, Python, Java,C#, or JavaScript background, this is probably the biggest hurdle: functions in bash are not functions, they are commands, and behave as such: they can output to stdout/stderr, they can pipe in/out, and they can return an exit code. Basically, there isn't any difference between defining a command in a script and creating an executable that can be called from the command line. So instead of writing your script like this: Top-level code Bunch of functions More top-level code write it like this: # Define your main, containing all top-level code main() Bunch of functions # Call main main where main() declares ret_val as local and all other functions return values via ret_val. See also the Unix & Linux question Scope of Local Variables in Shell Functions. Another, perhaps even better solution depending on situation, is the one posted by ya.teck which uses local -n. edited Mar 16, 2021 at 17:55 Peter Mortensen Oliver's user avatar Oliver # enddoc