Skip to content
shellmap

returnExit a shell function with a numeric status code across all 5 shells

Equivalents in every shell

Bashunix
return 1

Builtin. 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.

Zshunix
return 1

Same 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.

Fishunix
return 1

Fish 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.

PowerShellwindows
return 1

Exits 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.

cmd.exewindows
exit /b 1

Cmd 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

Bash
f() { return 1; }; f; echo $?
Zsh
f() { return 1; }; f; echo $?
Fish
function f; return 1; end; f; echo $status
PowerShell
function F { return 1 }; $r = F; $r

Return early from a function on bad input

Bash
f() { [ -z "$1" ] && return 1; echo "$1"; }
Zsh
f() { [[ -z "$1" ]] && return 1; echo "$1"; }
Fish
function f; if test -z "$argv[1]"; return 1; end; echo $argv[1]; end
PowerShell
function F { param($x) if (-not $x) { return }; $x }

Bash: return without argument propagates the last command status

Bash
f() { false; return; }; f; echo $?
Zsh
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".

WSL & PowerShell Core notes

pwshPowerShell's "everything is output" model means `return` is rarely needed for value-passing — only for early-exit logic. Cross-platform pwsh scripts behave identically on Windows, Linux, macOS. Bash scripts that `return $value` to "return a value" must be rewritten as `Write-Output $value; return` when ported.
WSLWSL bash `return` works exactly as native Linux. Capturing a WSL function's exit code from Windows pwsh requires running it as a script and inspecting `$LASTEXITCODE`: `wsl bash -c "f; exit $?"` — there is no PID-level signal that the WSL function returned a specific status.

Related commands