Two ways of running command in subshell
() runs commands in the subshell, so by exit you are exiting from subshell and returning to the parent shell.
Use braces {} if you want to run commands in the current shell.
From bash manual:
(list) list is executed in a subshell environment. Variable assignments and builtin commands that affect the shell's environment do not remain in effect after the command completes. The return status is the exit status of list.
{ list; } list is simply executed in the current shell environment. list must be terminated with a newline or semicolon. This is known as a group command. The return status is the exit status of list. Note that unlike the metacharacters ( and ), { and } are reserved words and must occur where a reserved word is permitted to be recognized. Since they do not cause a word break, they must be separated from list by whitespace or another shell metacharacter.
Reference: Why does (exit 1) not exit the script?
You might also like
Two ways of running command in subshell
shell
Modern Python Package Structure with pyproject.toml, uv, Scripts, and FastAPI
python
A concise guide to building a clean, modern Python project using pyproject.toml, uv, scripts, and FastAPI.
Managing Multiple GPG Keys and YubiKey Setup
security
A practical guide to managing multiple GPG private keys — exporting, importing, backing up, and securely storing them on a YubiKey for signing, encryption, and SSH authentication.