Shells in Debian
This article explains the roles of Bash, Dash, Zsh, Fish and other shells on Debian. The key distinction is between the interactive shell, the account login shell and the interpreter selected by a script's shebang.
For day-to-day commands see Debian Practical Shell Handbook, and for scripting see Shell Scripting.
1. What is a shell?
A shell is a command interpreter. It reads commands, expands variables and globs, handles pipelines/redirections and starts programs.
2. Important concepts
Login shell
The shell configured for a user account.
getent passwd "$USER"
Current shell
ps -p $$ -o comm=
$SHELL usually points to the login shell, not necessarily the currently running one.
/bin/sh
On Debian, /bin/sh normally points to Dash. It is intended for POSIX-style shell scripts.
Script interpreter
Defined by the shebang:
#!/bin/sh
or:
#!/usr/bin/env bash
3. How Debian uses shells
Interactive user sessions commonly use Bash. System scripts may use /bin/sh for POSIX portability and speed.
Do not assume every sh script supports Bash syntax.
4. Important shells
Common shells:
- Bash,
- Dash,
- Zsh,
- Fish,
- ksh/mksh,
- tcsh.
5. Bash
Bash is Debian's common interactive shell.
bash --version
Useful features include history, completion, arrays, [[ ... ]], functions, job control and process substitution.
6. Bash configuration
Common files:
~/.bashrc
~/.profile
~/.bash_profile
/etc/bash.bashrc
Interactive non-login Bash usually reads ~/.bashrc.
7. Dash
Dash is a small POSIX-oriented shell. On Debian it commonly implements /bin/sh.
Do not use Bash-only syntax under:
#!/bin/sh
8. sh in Debian
readlink -f /bin/sh
Typical result:
/usr/bin/dash
9. Zsh
sudo apt install zsh
zsh
Configuration:
~/.zshrc
10. Fish
sudo apt install fish
fish
Configuration:
~/.config/fish/config.fish
Fish intentionally differs from POSIX shell syntax.
11. ksh and mksh
sudo apt install ksh
sudo apt install mksh
12. tcsh
sudo apt install tcsh
Mostly encountered in legacy or specialized environments.
13. Available login shells
cat /etc/shells
14. Checking shell information
echo "$SHELL"
ps -p $$ -o comm=
getent passwd "$USER"
15. Running another shell
bash
zsh
fish
Exit with:
exit
16. exec
Replace the current shell process:
exec zsh
17. Changing login shell
chsh -s /usr/bin/zsh
Use a path listed in /etc/shells.
18. Shebang
Portable:
#!/bin/sh
Bash-specific:
#!/usr/bin/env bash
19. Do not change /bin/sh casually
Debian system scripts may depend on the system POSIX shell. Install another user shell instead of replacing /bin/sh.
20. Redirections and pipelines
command > file
command >> file
command 2> errors
command1 | command2
21. Variables
name="Alice"
echo "$name"
No spaces around =.
22. Quoting
echo '$HOME'
echo "$HOME"
Single quotes prevent expansion; double quotes allow variable expansion.
23. Globbing
*.txt
file?.log
[abc]*
24. Builtins and programs
type cd
type ls
command -V printf
25. Aliases
alias ll='ls -lah'
Use aliases for interactive convenience, not script dependencies.
26. Functions
mkcd() {
mkdir -p "$1" && cd "$1"
}
27. History
history
Reverse search:
Ctrl+R
28. Job control
command &
jobs
fg
bg
29. Login and interactive shells
These are separate concepts. Startup files differ depending on whether a shell is login, interactive or non-interactive.
30. PATH
echo "$PATH"
Common user-script location:
~/.local/bin
31. Practical choice
Interactive use: Bash or Zsh.
Portable scripts: /bin/sh.
Bash-specific scripts: #!/usr/bin/env bash.
Fish is excellent interactively but intentionally not POSIX-compatible.
32. Quick reference
echo "$SHELL"
ps -p $$ -o comm=
cat /etc/shells
readlink -f /bin/sh
chsh -s /usr/bin/zsh
exec bash
33. Most important rule
The interactive shell and a script's interpreter do not have to be the same. Always read the shebang.
Official references
- Debian Reference - Unix-like environment and shells: https://www.debian.org/doc/manuals/debian-reference/ch01.en.html
- Debian Reference - shell programming: https://www.debian.org/doc/manuals/debian-reference/ch12
- Debian manpages: https://manpages.debian.org/