What is the difference between sh, bash and dash shells?

When debugging a Debian-based Docker image, after entering the container and pressing the up arrow key in the terminal, the terminal displays ^[[A, the down arrow displays ^[[B, the right arrow displays ^[[C, and the left arrow displays ^[[D , pressing the delete key also displays several special characters. It's very strange, after a closer look, it turns out that the sh used by the terminal when entering the container can be switched to bash (you can switch to bash by typing the bash command in the terminal).

Both sh and bash are common Unix shells. In fact, there is another one called dash. Next, let’s look at the connections and differences between the three.

sh

sh is the abbreviation of Shell, which is the default shell of Unix/Linux system and one of the oldest shells. sh is the standard POSIX shell, and there are many different versions and implementations, such as the Bourne shell and the POSIX shell.

bash

bash is the abbreviation of Bourne-Again Shell, which is an enhanced version of sh with more functions and options. bash provides features such as command auto-completion, history, aliases, and job control. bash is the default shell on most Linux distributions and macOS systems.

dash

dash is the abbreviation of Debian Almquist shell, which is a lightweight shell derived from NetBSD and designed specifically for the Debian distribution. Compared to bash, dash has a leaner code and faster startup, but offers fewer features. On Debian systems, dash is usually used as /bin/sh (instead of bash) because dash is more POSIX compliant and starts faster.

In many Linux, /bin/sh points to /bin/bash, that is, /bin/sh is a soft link of /bin/bash

# ls -l /bin/sh
lrwxrwxrwx 1 root root 4 Oct 15  2017 /bin/sh -> bash

On Debian systems and Debian-based distributions, /bin/sh points to /bin/dash

# ls -l /bin/sh
lrwxrwxrwx 1 root root 4 Oct 15  2017 /bin/sh -> dash

The method of switching sh to use bash instead of dash is also very simple, because /bin/sh is a soft link, just change it to point to /bin/bash, execute the following command

# ln -sf /bin/bash /bin/sh

If you want to switch to dash, execute the following command

# ln -sf /bin/dash /bin/sh

If you are making a Docker image and want to use bash by default after entering a container based on this image, you can add the following line to the corresponding Dockerfile

RUN ln -sf /bin/bash /bin/sh

If the virtual machine uses sh or dash by default, if you want to use bash by default after logging in, you can use the chsh command to change the default shell type of the specified user. The command is as follows:

chsh -s /bin/bash user

Change the user in the command to your own user name, and the next time you log in, bash will be used as the default shell terminal type.

Guess you like

Origin blog.csdn.net/luduoyuan/article/details/131152648