return

noun

say it: “ri-TURN” /rɪˈtɝn/

A bash command that ends a function and sets its exit code: 0 for yes and 1 to 255 for no, which if can test.

What does return do in bash?

return ends a function and sets its exit code: 0 means yes, it worked, and 1 to 255 mean no, ready for if to test. It can't hand back a result such as a total; for that, the function prints it with echo and the caller catches it with $( ).

fits() {
  if [[ $1 -gt $2 ]]; then return 1; fi
  return 0
}
if fits 25 45; then echo "The mix fits the drive."; fi   # The mix fits the drive.

In a sentence

  • “The fits function uses return 1 when the mix is longer than the drive.”
  • “Sam tried to hand back 300 with return and got 44, so he printed the total with echo instead.”

Level: Journeyman · More Unix terms at this level

Updated