0

there is a simple bash shell fragment

function gettop()
{

 abc= /bin/pwd

}

funret=$(gettop)
echo $funret

the output of funret is the ouput of command /bin/pwd:

/home/xxx/xxx

how does this happen?

according to shell standard, function return either with a echo or return, but here non of these involved; but the result show me that getopt returned /bin/pwd and $() just run this command.

another thing is, after I delete the extra empty space after abc= the output will become null;

what is going on here exactly?

Richard Shi
  • 53
  • 1
  • 4

1 Answers1

0

The output i.e. /home/xxx/xxx is an expected behavior because in shell the variables are assigned without space in them i.e. abc=/bin/pwd. Now since you have defined the variable using space shell tried to execute the string after the space during the function and the output /home/xxx/xxx is because of that execution as /bin/pwd is a know command to shell.

If you use the below code snippet it would throw an error.

function gettop()
 {
abc= testing
}
funret=$(gettop)
echo $funret

Error:

./tmp.sh: line 5: testing: command not found

error404
  • 2,684
  • 2
  • 13
  • 21