We often use sh
command to execute some files with the extension .sh in Linux.
These shell script files which can complete various tasks by writing commands and executed by interpreter, such as data processing, automated execution, management of system resources ... etc.
But sometimes, after we wrote the commands in the script with the commands on the Internet, we execute the script, but errors occur.
However, it is very likely that the script will run smoothly after executing the file with bash
instead.
What is the difference between sh
and bash
?
On most systems, /bin/sh
should be linked to bash through a soft link. At this time, if we use sh to execute the file, it is equivalent to executing the file through the POSIX standard mode of bash.
So why sometimes sh
is not equal to bash
?
Many times, this is because Ubuntu has used dash
(Debian Almquist Shell) instead of bash
(Bourne again / born again) by default since a certain version.
What is dash ?
Dash
is a relatively lightweight and fast POSIX standard Shell, which allows the system to execute various system commands more quickly. After all, the original bash is more complicated.
Then, the biggest problem with dash is that it is not as convenient as bash
when executing script files, and there are some compatibility issues.
What is bash ?
Bash
is a very versatile shell, and its history is quite long. The following is a direct excerpt from the introduction at the beginning of WIKI:
—— WIKI
Bash is a Unix shell and command language written by Brian Fox for the GNU Project as a free software replacement for the Bourne shell.[11][12] First released in 1989,[13] it has been used as the default login shell for most Linux distributions.[14] A version is also available for Windows 10 via the Windows Subsystem for Linux.[15] It is also the default user shell in Solaris 11.[16] Bash was also the default shell in all versions of Apple macOS prior to the 2019 release of macOS Catalina, which changed the default shell to zsh, although Bash currently remains available as an alternative shell.[17]
Bash is a command processor that typically runs in a text window where the user types commands that cause actions. Bash can also read and execute commands from a file, called a shell script. Like most Unix shells, it supports filename globbing (wildcard matching), piping, here documents, command substitution, variables, and control structures for condition-testing and iteration. The keywords, syntax, dynamically scoped variables and other basic features of the language are all copied from sh. Other features, e.g., history, are copied from csh and ksh. Bash is a POSIX-compliant shell, but with a number of extensions.
The shell's name is an acronym for Bourne Again Shell, a pun on the name of the Bourne shell that it replaces[18] and the notion of being "born again".[19][20]
A security hole in Bash dating from version 1.03 (August 1989),[21] dubbed Shellshock, was discovered in early September 2014 and quickly led to a range of attacks across the Internet.[22][23][24] Patches to fix the bugs were made available soon after the bugs were identified.
Switch sh link to "dash" or "bash"
We can use the following command to view which one is the shell currently connected.
ls -l /bin/sh
You may see the following screen:
If you want to switch to bash
, you can use the following command:
sudo dpkg-reconfigure dash
And Select.
- Select Yes, then
/bin/sh
will link todash
; - Select No, then
/bin/sh
will link tobash
.
References
- https://wiki.ubuntu.com/DashAsBinSh
- https://askubuntu.com/questions/1064773/how-can-i-make-bin-sh-point-to-bin-bash
- https://unix.stackexchange.com/questions/442510/how-to-use-bash-for-sh-in-ubuntu
- https://www.howtogeek.com/669835/how-to-change-your-default-shell-on-linux-with-chsh/