How to determine the current interactive shell that I'm in (command-line)
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Determining which shell you are currently using is important for writing compatible scripts, setting up dotfiles, and debugging environment issues. The most reliable method is ps -p $$ -o comm=, which queries the process table for the current shell process. Other methods like echo $0, $SHELL, and shell-specific variables each have caveats. $SHELL tells you the default login shell, not necessarily the shell you are currently running in.
Quick Methods
ps -p $$ (Most Reliable)
$$ is the PID of the current shell process. ps -p $$ -o comm= prints just the command name without headers. This works in bash, zsh, sh, dash, and ksh.
echo $0
$0 contains the name or path of the current shell. A leading - indicates a login shell. This works in most POSIX shells but not in fish.
$SHELL (Default Shell, Not Current)
$SHELL is set to your login shell from /etc/passwd or Directory Services. It does NOT change when you switch shells. If your default is zsh but you run bash, $SHELL still says /bin/zsh.
Shell-Specific Variables
Each shell sets unique variables you can test:
Detection Script
Checking Shell Features
Methods on Different Systems
macOS
Linux
Within a Script
Login Shell vs Non-Login Shell
Login shells read different config files:
- bash login:
~/.bash_profileor~/.profile - bash non-login:
~/.bashrc - zsh login:
~/.zprofile,~/.zshrc - zsh non-login:
~/.zshrc
Common Pitfalls
- Relying on
$SHELLfor the current shell: $SHELLis the default login shell, not the currently running shell. If you switched shells withbashorzsh,$SHELLstill shows the original. - Assuming
$0always returns the shell name: Inside scripts, $0is the script filename. In subshells, it may be-bash(with a leading dash) for login shells. Parse carefully. - Fish shell incompatibility: Fish does not support POSIX syntax (
$?,$$,[ -n ]). Useecho $FISH_VERSIONorstatus fish-pathfor detection in fish. - Confusing sh with bash: On many systems,
/bin/shis a symlink tobashordash. Runningshmay invoke bash in POSIX compatibility mode, which disables bash-specific features like arrays. - Not checking the shell before using shell-specific features: Bashisms like
[[ ]],(( )), and arrays break insh,dash, and other POSIX-only shells. Always check the shell or use POSIX-compatible syntax for portable scripts.
Summary
ps -p $$ -o comm=is the most reliable way to determine the current shell$SHELLshows your default login shell, not the currently running shell- Shell-specific variables (
$BASH_VERSION,$ZSH_VERSION, $FISH_VERSION) confirm both the shell and its version $0shows the shell name but includes a leading-for login shells and shows script names inside scripts- Always test for the current shell before using shell-specific features in portable scripts
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.