return — Exit a shell function with a numeric status code across all 5 shells
Equivalents in every shell
return 1Builtin. Valid ONLY inside functions or `source`d scripts. Argument is a numeric exit status (0–255). Without an argument, `return` propagates the exit status of the LAST command run inside the function.
return 1Same as bash. Inside a `TRAPSIGNAL`-style function, `return` from the trap resumes the outer script normally. Like bash, the argument is silently masked to 8 bits.
return 1Fish builtin. Used inside `function ... end` blocks. Argument is a numeric exit status (no 8-bit mask — fish stores the full int). Without an argument, returns 0.
return 1Exits the current function/script BLOCK and SENDS its argument to the output stream. Different from bash: the argument is the return VALUE (any type), not just a status code. The exit code surfaces in `$LASTEXITCODE` only for external commands; for functions, capture the result.
exit /b 1Cmd has no `return`. Use `exit /b N` inside a `call`ed label to exit the label with errorlevel N; `exit /b` without a number returns the last command's errorlevel. Plain `exit 1` (no `/b`) closes the ENTIRE cmd.exe process.
Worked examples
Return an error status from a function
f() { return 1; }; f; echo $?f() { return 1; }; f; echo $?function f; return 1; end; f; echo $statusfunction F { return 1 }; $r = F; $rReturn early from a function on bad input
f() { [ -z "$1" ] && return 1; echo "$1"; }f() { [[ -z "$1" ]] && return 1; echo "$1"; }function f; if test -z "$argv[1]"; return 1; end; echo $argv[1]; endfunction F { param($x) if (-not $x) { return }; $x }Bash: return without argument propagates the last command status
f() { false; return; }; f; echo $?f() { false; return; }; f; echo $?Gotchas
- Bash `return` outside of a function or `source`d script is an ERROR — the interactive shell prints "return: can only `return` from a function or sourced script". Use `exit` for top-level scripts; `return` is strictly a function/sourced-script primitive.
- Bash `return 1000` does NOT preserve `1000` — exit statuses are 8 bits modulo 256. `return 256` becomes `return 0` silently; `return -1` becomes `return 255`. Stick to the 0–255 range and treat `return` as a status, not a numeric value.
- PowerShell `return $x` SENDS `$x` to the output pipeline AND exits the function. `return` with no arg only exits — but any prior uncaptured statements have ALREADY emitted their results. `return` is not a "discard pending output" command; pipe to `| Out-Null` for that.
- Fish `return` inside a non-function (e.g. a sourced file at top level) does NOT error — it just no-ops or returns from the surrounding context. Different from bash, which would refuse. Don't rely on `return` to abort a sourced fish config — use `exit` if you want hard abort.
- Cmd `exit /b 1` REQUIRES the `/b` flag. Plain `exit 1` exits the ENTIRE cmd.exe process — closing terminals and aborting outer batch scripts that called yours. Always use `exit /b` inside `call`able sub-procedures; reserve plain `exit` for "shut down this cmd window".