In my program I am trying to return a value from a function, the return value is string. Everything works fine(atleast some part), if I echo the value once it is returned, but it is not even calling the function, if I dont return…. Consider the code below….
#!/bin/bash
function get_last_name() {
echo "Get Last Name"
ipath=$1
IFS='/'
set $ipath
for item
do
last=$item
done
echo $last
}
main() {
path='/var/lib/iscsi/ifaces/iface0'
current=$(get_last_name "$path")
echo -n "Current="
echo $current
}
main
It gives me an output like this
OUTPUT
Current=Get Last Name iface0
If I comment the echo $current, then the I am not even seeing the “Get Last Name”, which makes to come to conclusion, that it is not even calling the function. Please let me know what mistake I am making. But one thing I am sure, bash is the ugliest language I have ever seen…….
Functions do not have return values in bash. When you write
you are not assigning a return value to
current. You are capturing the standard output ofget_last_name(written using theechocommand) and assigning it tocurrent. That’s why you don’t see “Get last name”; that text does not make it to the terminal, but is stored incurrent.Detailed explanation
Let’s walk through
get_last_namefirst (with some slight modifications to simplify the explanation):I added the
localcommand beforeIFSso that the change is confined to the body ofget_last_name, and I moved the firstechoto the end to emphasize the similarity between the twoechostatements. Whenget_last_nameis called, it processes its single argument (a string containing a file path), then echoes two strings: “Get Last Name” and the final component of the file path. If you were to run execute this function from the command line, it would appear something like this:The exit code of the function would be the exit code of the last command executed, in this case
echo $last. This will be 0 as long as the write succeeds (which it almost certainly will).Now, we look at the function
main, which callsget_last_name:Just like with
get_last_name,mainwill not have a return value; it will produce an exit code which is the exit code ofecho $current. The function begins by callingget_last_nameinside a command substitution ($(...)), which will capture all the standard output fromget_last_nameand treat it as a string.DIGRESSION
Note the difference between the following:
sets the value of
currentto the accumulated standard output ofget_last_name. (Among other things, newlines in the output are replaced with spaces, but the full explanation of how whitespace is handled is a topic for another day). This has nothing to do with return values; remember, the exit code (the closet thingbashhas to “return values”) is a single integer.would not even call
get_last_name. It would interpret “$path” as the name of a command and try to execute it. That command would have a variablecurrentwith the string value “get_last_name” in its environment.The point being,
get_last_namedoesn’t “return” anything that you can assign to a variable. It has an exit code, and it can write to standard output. The$(...)construct lets you capture that output as a string, which you can then (among other things) assign to a variable.Back to
mainOnce the value of
currentis set to the output generated byget_last_name, we executetwo last
echostatements to write to standard output again. The first writes “Current=” without a newline, so that the nextechostatement produces text on the same line as the first. The second just echoes the value ofcurrent.When you commented out the last
echoofmain, you didn’t stopget_last_namefrom being executed (it had already been executed). Rather, you just didn’t print the contents of thecurrentvariable, where the output ofget_last_namewas placed rather than on the terminal.