I was reading about traps, but only return works for me in my shell script, so was wondering what status or code it returns, so what I tried is,
#!/bin/bash
seeOutput=`return`
echo $seeOutput
It's just returning a new line and when done on terminal, it says,
-bash: return: can only `return' from a function or sourced script
which I already know :p I just need to know "return"'s exit status.
return
defaults to true. bash 4.3 has includedreturn -1
which means error. Thusreturn
accept negative values as return value (e.g. return -1 will show as (8 bit) 255 in the caller). See wiki.bash-hackers.org/scripting/bashchangesreturn
defaults toreturn "$?"
(that is, it returns with the exit status of the last run command).return
on its own indicates true as you'd do in creturn 0
to indicate success. Anything else will be false and ofcourse based on the status of$?
returned from the last command. e.gf(){ args=2; [[ $# = $args ]] && return || return ; }; f 1
Will hit the secondreturn
which will be false. Howeverf(){ args=2; [[ $# = $args ]] && return || return ; }; f 1 2
will hit the firstreturn
which will be true.return
from a function or a sourced script, why are you calling it in another context? What do you expect to happen? And what do you mean by “ "return"'s exit status”? Sincereturn
causes its context to exit, there's no way to observe any exit status of thereturn
instruction itself, what you observe is the exit status of the containing function or sourced script.