What a Shell Is
When you log in to a server over SSH, the server starts one shell for your session and shows you its prompt. When a script starts with a line such as #!/bin/bash, that first line names the shell that runs it. POSIX is the standard that defines a portable shell; sh, dash, bash, zsh, and ksh follow most of it, while fish and csh do not.
The Common Shells Compared
| Shell | Where you meet it | Notes |
|---|---|---|
sh | Scripts, /bin/sh | The POSIX shell; a link to another shell. |
bash | Most server logins | The usual default; the base of most guides. |
dash | Debian, Ubuntu | Small and fast; POSIX features only. |
zsh | Interactive use | Strong completion; close to bash. |
fish | Interactive use | Helpful defaults; its own syntax. |
ksh | Older scripts | The KornShell; inspired many bash features. |
sh and dash
/bin/sh is not a shell of its own on current systems but a link to one. On Debian and Ubuntu it points to dash, which starts quickly and supports only POSIX features. On AlmaLinux and Rocky Linux it points to bash. Check where it leads on your server:
readlink -f /bin/sh
This explains a common surprise: a script that starts with #!/bin/sh but uses bash features, such as [[ ]] tests or arrays, works on one server and fails on another. If a script needs bash, start it with #!/bin/bash.
bash
Bash, the Bourne Again Shell, is the login shell that adduser gives new users on Debian and Ubuntu and that useradd gives them on AlmaLinux and Rocky Linux, and most server documentation assumes it. On Debian and Ubuntu, useradd without -s /bin/bash can leave a user with /bin/sh instead. It adds arrays, [[ ]] tests, brace expansion, and command history to the POSIX base. Its per-user settings live in ~/.bashrc; see what .bashrc is and how to edit it. For scripting, see the Bash case statement and continue and break in Bash loops.
zsh and fish
- zsh: close to bash in daily use, with stronger tab completion, spelling correction, and themable prompts. Install it with
sudo apt install zshorsudo dnf install zsh. - fish: suggestions as you type and syntax highlighting without any configuration. Its scripting language is its own: for example,
set -x NAME valueexports a variable, and many bash scripts do not run in it. Use it at the prompt and keep your scripts in bash.
ksh, csh, and Others
The KornShell (ksh) came before bash and gave it several features; you find it in older scripts. The C shell (csh) and its successor tcsh use a C-like syntax and are rare on Linux servers. Install them from your distribution's repositories when a script needs one.
Find Out Which Shell You Are Using
echo "$SHELL"
echo "$0"
ps -p $$
$SHELL holds your login shell, the one set for your account. It does not change when you start another shell inside the session. $0 and ps -p $$ show the shell that is running right now. A leading dash in the output of echo "$0", as in -bash, marks a login shell.
List the Installed Shells
cat /etc/shells
This file lists the shells allowed as login shells. Installing zsh or fish with the package manager normally adds them to it.
Change Your Login Shell with chsh
Keep your current SSH session open and test the new shell in a second session before you close the first. A login shell that fails to start leaves you without a prompt.
chsh -s /usr/bin/zsh
sudo chsh -s /bin/bash deploy
The first command changes your own shell and asks for your password. The second changes the shell of the user deploy. Use a path exactly as it appears in /etc/shells. The change applies at the next login. Leave root on bash: most recovery instructions assume it.
Login, Interactive, and Non-Interactive Shells
- Login shell: started when you log in, for example over SSH. Bash reads
/etc/profile, then the first of~/.bash_profile,~/.bash_login, and~/.profilethat it can read. - Interactive non-login shell: started when you type
bashinside a session. Bash reads~/.bashrc. - Non-interactive shell: runs a script or a cron job and reads neither file by default. That is why a variable set in
~/.bashrcis missing in a cron job. Commands sent over SSH without a terminal are a special case, covered in the .bashrc guide.