What is the difference between #!/bin/sh and #!/bin/bash?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
if I write,
#!/bin/bash
echo "foo"
or
#!/bin/sh
echo "foo"
both yields same. I have seen some scripts starting with #!/bin/sh
or #!/bin/bash
. Is there any difference between them?
bash scripts sh shebang
add a comment |
if I write,
#!/bin/bash
echo "foo"
or
#!/bin/sh
echo "foo"
both yields same. I have seen some scripts starting with #!/bin/sh
or #!/bin/bash
. Is there any difference between them?
bash scripts sh shebang
add a comment |
if I write,
#!/bin/bash
echo "foo"
or
#!/bin/sh
echo "foo"
both yields same. I have seen some scripts starting with #!/bin/sh
or #!/bin/bash
. Is there any difference between them?
bash scripts sh shebang
if I write,
#!/bin/bash
echo "foo"
or
#!/bin/sh
echo "foo"
both yields same. I have seen some scripts starting with #!/bin/sh
or #!/bin/bash
. Is there any difference between them?
bash scripts sh shebang
bash scripts sh shebang
edited Feb 11 at 10:05
dessert
25.5k674108
25.5k674108
asked May 25 '12 at 3:59
Rahul VirparaRahul Virpara
6,873103447
6,873103447
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
bash
and sh
are two different shells. Basically bash
is sh
, with more features and better syntax. Most commands work the same, but they are different.
Having said that, you should realize /bin/sh
on most systems will be a symbolic link and will not invoke sh
. In Ubuntu /bin/sh
used to link to bash
, typical behavior on Linux distributions, but now has changed to linking to another shell called dash. I would use bash
, as that is pretty much the standard (or at least most common, from my experience). In fact, problems arise when a bash script will use #!/bin/sh
because the script-maker assumes the link is to bash
when it doesn't have to be.
For more info, http://man.cx/sh, http://man.cx/bash.
21
Your first paragraph is quite misleading. "sh" is not a shell at all, but a symlink to the currently configured system shell, which on Linux systems is usually either bash or dash (recent versions of Ubuntu using the latter).
– thomasrutter
Sep 5 '12 at 1:16
16
/bin/sh
used to be a shell called bourne shell back in 1977. bash is a /bin/sh compatible shell that can be used as a bourne shell replacement that is posix conforming. en.wikipedia.org/wiki/Bourne_shell
– Alex
Sep 18 '14 at 6:57
2
bash is a de facto standard (very widely used) but it is not "standard"; sh on Ubuntu uses dash, which is a Posix-compliant shell, so it is really standard. My advice is to use dash as often as possible for scripting, especially for server-side scripts. Although bash is more expressive, dash runs quite a lot faster and is more secure.
– Rick-777
Sep 29 '14 at 17:19
@Rick-777 So should I explicitly put #!/bin/dash at the top of my scripts? I have also written scripts where I only write commands and execute with ./scriptName and that has worked fine. Is the #!/bin/yourShellHere necessary?
– user137717
Aug 27 '15 at 20:42
@Rick-777 In the cases where I exclude any #!/bin/shellName, I have named the files fileName.sh. Will that implicitly link to the preferred system shell without declaring #!/bin/sh?
– user137717
Aug 27 '15 at 20:44
|
show 1 more comment
On Linux and other Unix-like systems you have a choice of multiple shells.
The shell is responsible not only for drawing your little prompt, but interpreting your commands, especially if you put in complicated logic like pipes, conditionals and so on.
bash is the most common shell used as a default shell for users of Linux systems. It is a spiritual descendent of other shells used throughout Unix history. Its name, bash is an abbreviation of Bourne-Again Shell, an homage to the Bourne shell it was designed to replace, though it also incorporates features from the C Shell and the Korn Shell.
It's run, these days, from /bin/bash
- any system with bash will have it accessible here.
It's not just users that use shells, though. Scripts (shell scripts) need shells to interpret them. When you run a shell script, your system needs to start up a shell process to execute your script.
The problem is, different shells have tiny little inconsistencies between them, and when it comes to running scripts, these can be a real problem. bash has quite a lot of scripting features that are unique only to bash, and not to other shells. This is fine, if you're always going to use bash to run those scripts. Other shells may try to either emulate bash, or adhere to the POSIX standard, which bash supports pretty well (though adds its own extensions to).
It's possible to specify at the top of a shell script which shell it should be run with using a shebang. A script may specify #!/bin/bash
on the first line, meaning that the script should always be run with bash, rather than another shell.
/bin/sh is an executable representing the system shell. Actually, it is usually implemented as a symbolic link pointing to the executable for whichever shell is the system shell. The system shell is kind of the default shell that system scripts should use. In Linux distributions, for a long time this was usually a symbolic link to bash, so much so that it has become somewhat of a convention to always link /bin/sh to bash or a bash-compatible shell. However, in the last couple of years Debian (and Ubuntu) decided to switch the system shell from bash to dash - a similar shell - breaking with a long tradition in Linux (well, GNU) of using bash for /bin/sh. Dash is seen as a lighter, and much faster, shell which can be beneficial to boot speed (and other things that require a lot of shell scripts, like package installation scripts).
Dash is fairly well compatible with bash, being based on the same POSIX standard. However, it doesn't implement the bash-specific extensions. There are scripts in existence that use #!/bin/sh
(the system shell) as their shebang, but which require bash-specific extensions. This is currently considered a bug that should be fixed by Debian and Ubuntu, who require /bin/sh to be able to work when pointed to dash.
Even though Ubuntu's system shell is pointing to dash, your login shell as a user continues to be bash at this time. That is, when you log in to a terminal emulator anywhere in Linux, your login shell will be bash. Speed of operation is not so much a problem when the shell is used interactively, and users are familiar with bash (and may have bash-specific customisations in their home directory).
What you should use when writing scripts
If your script requires features only supported by bash, use #!/bin/bash
.
But if at all possible, it would be good to make sure your script is POSIX-compatible, and use #!/bin/sh
, which should always, quite reliably, point to the preferred POSIX-compatible system shell in any installation.
add a comment |
In addition to the previous answers, even if /bin/sh
is a symbolic link to /bin/bash
, #!/bin/sh
is not totally equivalent to #!/bin/bash
.
From the bash(1) man page :
"If bash is invoked with the name sh, it tries to mimic the startup
behavior of historical versions of sh as closely as possible, while
conforming to the POSIX standard as well."
For example the bash specific syntax :
exec > >(tee logfile.txt)
gives an error in a shell beginning with #!/bin/sh
, even with the sh->bash symbolic link in place.
add a comment |
GNU Bash: #!/bin/bash
;
POSIX shell: #!/bin/sh
.
Well, short but accurate answer. 还行
– Sergiy Kolodyazhnyy
Dec 24 '18 at 11:11
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "89"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f141928%2fwhat-is-the-difference-between-bin-sh-and-bin-bash%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
bash
and sh
are two different shells. Basically bash
is sh
, with more features and better syntax. Most commands work the same, but they are different.
Having said that, you should realize /bin/sh
on most systems will be a symbolic link and will not invoke sh
. In Ubuntu /bin/sh
used to link to bash
, typical behavior on Linux distributions, but now has changed to linking to another shell called dash. I would use bash
, as that is pretty much the standard (or at least most common, from my experience). In fact, problems arise when a bash script will use #!/bin/sh
because the script-maker assumes the link is to bash
when it doesn't have to be.
For more info, http://man.cx/sh, http://man.cx/bash.
21
Your first paragraph is quite misleading. "sh" is not a shell at all, but a symlink to the currently configured system shell, which on Linux systems is usually either bash or dash (recent versions of Ubuntu using the latter).
– thomasrutter
Sep 5 '12 at 1:16
16
/bin/sh
used to be a shell called bourne shell back in 1977. bash is a /bin/sh compatible shell that can be used as a bourne shell replacement that is posix conforming. en.wikipedia.org/wiki/Bourne_shell
– Alex
Sep 18 '14 at 6:57
2
bash is a de facto standard (very widely used) but it is not "standard"; sh on Ubuntu uses dash, which is a Posix-compliant shell, so it is really standard. My advice is to use dash as often as possible for scripting, especially for server-side scripts. Although bash is more expressive, dash runs quite a lot faster and is more secure.
– Rick-777
Sep 29 '14 at 17:19
@Rick-777 So should I explicitly put #!/bin/dash at the top of my scripts? I have also written scripts where I only write commands and execute with ./scriptName and that has worked fine. Is the #!/bin/yourShellHere necessary?
– user137717
Aug 27 '15 at 20:42
@Rick-777 In the cases where I exclude any #!/bin/shellName, I have named the files fileName.sh. Will that implicitly link to the preferred system shell without declaring #!/bin/sh?
– user137717
Aug 27 '15 at 20:44
|
show 1 more comment
bash
and sh
are two different shells. Basically bash
is sh
, with more features and better syntax. Most commands work the same, but they are different.
Having said that, you should realize /bin/sh
on most systems will be a symbolic link and will not invoke sh
. In Ubuntu /bin/sh
used to link to bash
, typical behavior on Linux distributions, but now has changed to linking to another shell called dash. I would use bash
, as that is pretty much the standard (or at least most common, from my experience). In fact, problems arise when a bash script will use #!/bin/sh
because the script-maker assumes the link is to bash
when it doesn't have to be.
For more info, http://man.cx/sh, http://man.cx/bash.
21
Your first paragraph is quite misleading. "sh" is not a shell at all, but a symlink to the currently configured system shell, which on Linux systems is usually either bash or dash (recent versions of Ubuntu using the latter).
– thomasrutter
Sep 5 '12 at 1:16
16
/bin/sh
used to be a shell called bourne shell back in 1977. bash is a /bin/sh compatible shell that can be used as a bourne shell replacement that is posix conforming. en.wikipedia.org/wiki/Bourne_shell
– Alex
Sep 18 '14 at 6:57
2
bash is a de facto standard (very widely used) but it is not "standard"; sh on Ubuntu uses dash, which is a Posix-compliant shell, so it is really standard. My advice is to use dash as often as possible for scripting, especially for server-side scripts. Although bash is more expressive, dash runs quite a lot faster and is more secure.
– Rick-777
Sep 29 '14 at 17:19
@Rick-777 So should I explicitly put #!/bin/dash at the top of my scripts? I have also written scripts where I only write commands and execute with ./scriptName and that has worked fine. Is the #!/bin/yourShellHere necessary?
– user137717
Aug 27 '15 at 20:42
@Rick-777 In the cases where I exclude any #!/bin/shellName, I have named the files fileName.sh. Will that implicitly link to the preferred system shell without declaring #!/bin/sh?
– user137717
Aug 27 '15 at 20:44
|
show 1 more comment
bash
and sh
are two different shells. Basically bash
is sh
, with more features and better syntax. Most commands work the same, but they are different.
Having said that, you should realize /bin/sh
on most systems will be a symbolic link and will not invoke sh
. In Ubuntu /bin/sh
used to link to bash
, typical behavior on Linux distributions, but now has changed to linking to another shell called dash. I would use bash
, as that is pretty much the standard (or at least most common, from my experience). In fact, problems arise when a bash script will use #!/bin/sh
because the script-maker assumes the link is to bash
when it doesn't have to be.
For more info, http://man.cx/sh, http://man.cx/bash.
bash
and sh
are two different shells. Basically bash
is sh
, with more features and better syntax. Most commands work the same, but they are different.
Having said that, you should realize /bin/sh
on most systems will be a symbolic link and will not invoke sh
. In Ubuntu /bin/sh
used to link to bash
, typical behavior on Linux distributions, but now has changed to linking to another shell called dash. I would use bash
, as that is pretty much the standard (or at least most common, from my experience). In fact, problems arise when a bash script will use #!/bin/sh
because the script-maker assumes the link is to bash
when it doesn't have to be.
For more info, http://man.cx/sh, http://man.cx/bash.
edited May 25 '12 at 7:57
Chan-Ho Suh
6,79923169
6,79923169
answered May 25 '12 at 4:08
reverendj1reverendj1
12.6k23637
12.6k23637
21
Your first paragraph is quite misleading. "sh" is not a shell at all, but a symlink to the currently configured system shell, which on Linux systems is usually either bash or dash (recent versions of Ubuntu using the latter).
– thomasrutter
Sep 5 '12 at 1:16
16
/bin/sh
used to be a shell called bourne shell back in 1977. bash is a /bin/sh compatible shell that can be used as a bourne shell replacement that is posix conforming. en.wikipedia.org/wiki/Bourne_shell
– Alex
Sep 18 '14 at 6:57
2
bash is a de facto standard (very widely used) but it is not "standard"; sh on Ubuntu uses dash, which is a Posix-compliant shell, so it is really standard. My advice is to use dash as often as possible for scripting, especially for server-side scripts. Although bash is more expressive, dash runs quite a lot faster and is more secure.
– Rick-777
Sep 29 '14 at 17:19
@Rick-777 So should I explicitly put #!/bin/dash at the top of my scripts? I have also written scripts where I only write commands and execute with ./scriptName and that has worked fine. Is the #!/bin/yourShellHere necessary?
– user137717
Aug 27 '15 at 20:42
@Rick-777 In the cases where I exclude any #!/bin/shellName, I have named the files fileName.sh. Will that implicitly link to the preferred system shell without declaring #!/bin/sh?
– user137717
Aug 27 '15 at 20:44
|
show 1 more comment
21
Your first paragraph is quite misleading. "sh" is not a shell at all, but a symlink to the currently configured system shell, which on Linux systems is usually either bash or dash (recent versions of Ubuntu using the latter).
– thomasrutter
Sep 5 '12 at 1:16
16
/bin/sh
used to be a shell called bourne shell back in 1977. bash is a /bin/sh compatible shell that can be used as a bourne shell replacement that is posix conforming. en.wikipedia.org/wiki/Bourne_shell
– Alex
Sep 18 '14 at 6:57
2
bash is a de facto standard (very widely used) but it is not "standard"; sh on Ubuntu uses dash, which is a Posix-compliant shell, so it is really standard. My advice is to use dash as often as possible for scripting, especially for server-side scripts. Although bash is more expressive, dash runs quite a lot faster and is more secure.
– Rick-777
Sep 29 '14 at 17:19
@Rick-777 So should I explicitly put #!/bin/dash at the top of my scripts? I have also written scripts where I only write commands and execute with ./scriptName and that has worked fine. Is the #!/bin/yourShellHere necessary?
– user137717
Aug 27 '15 at 20:42
@Rick-777 In the cases where I exclude any #!/bin/shellName, I have named the files fileName.sh. Will that implicitly link to the preferred system shell without declaring #!/bin/sh?
– user137717
Aug 27 '15 at 20:44
21
21
Your first paragraph is quite misleading. "sh" is not a shell at all, but a symlink to the currently configured system shell, which on Linux systems is usually either bash or dash (recent versions of Ubuntu using the latter).
– thomasrutter
Sep 5 '12 at 1:16
Your first paragraph is quite misleading. "sh" is not a shell at all, but a symlink to the currently configured system shell, which on Linux systems is usually either bash or dash (recent versions of Ubuntu using the latter).
– thomasrutter
Sep 5 '12 at 1:16
16
16
/bin/sh
used to be a shell called bourne shell back in 1977. bash is a /bin/sh compatible shell that can be used as a bourne shell replacement that is posix conforming. en.wikipedia.org/wiki/Bourne_shell– Alex
Sep 18 '14 at 6:57
/bin/sh
used to be a shell called bourne shell back in 1977. bash is a /bin/sh compatible shell that can be used as a bourne shell replacement that is posix conforming. en.wikipedia.org/wiki/Bourne_shell– Alex
Sep 18 '14 at 6:57
2
2
bash is a de facto standard (very widely used) but it is not "standard"; sh on Ubuntu uses dash, which is a Posix-compliant shell, so it is really standard. My advice is to use dash as often as possible for scripting, especially for server-side scripts. Although bash is more expressive, dash runs quite a lot faster and is more secure.
– Rick-777
Sep 29 '14 at 17:19
bash is a de facto standard (very widely used) but it is not "standard"; sh on Ubuntu uses dash, which is a Posix-compliant shell, so it is really standard. My advice is to use dash as often as possible for scripting, especially for server-side scripts. Although bash is more expressive, dash runs quite a lot faster and is more secure.
– Rick-777
Sep 29 '14 at 17:19
@Rick-777 So should I explicitly put #!/bin/dash at the top of my scripts? I have also written scripts where I only write commands and execute with ./scriptName and that has worked fine. Is the #!/bin/yourShellHere necessary?
– user137717
Aug 27 '15 at 20:42
@Rick-777 So should I explicitly put #!/bin/dash at the top of my scripts? I have also written scripts where I only write commands and execute with ./scriptName and that has worked fine. Is the #!/bin/yourShellHere necessary?
– user137717
Aug 27 '15 at 20:42
@Rick-777 In the cases where I exclude any #!/bin/shellName, I have named the files fileName.sh. Will that implicitly link to the preferred system shell without declaring #!/bin/sh?
– user137717
Aug 27 '15 at 20:44
@Rick-777 In the cases where I exclude any #!/bin/shellName, I have named the files fileName.sh. Will that implicitly link to the preferred system shell without declaring #!/bin/sh?
– user137717
Aug 27 '15 at 20:44
|
show 1 more comment
On Linux and other Unix-like systems you have a choice of multiple shells.
The shell is responsible not only for drawing your little prompt, but interpreting your commands, especially if you put in complicated logic like pipes, conditionals and so on.
bash is the most common shell used as a default shell for users of Linux systems. It is a spiritual descendent of other shells used throughout Unix history. Its name, bash is an abbreviation of Bourne-Again Shell, an homage to the Bourne shell it was designed to replace, though it also incorporates features from the C Shell and the Korn Shell.
It's run, these days, from /bin/bash
- any system with bash will have it accessible here.
It's not just users that use shells, though. Scripts (shell scripts) need shells to interpret them. When you run a shell script, your system needs to start up a shell process to execute your script.
The problem is, different shells have tiny little inconsistencies between them, and when it comes to running scripts, these can be a real problem. bash has quite a lot of scripting features that are unique only to bash, and not to other shells. This is fine, if you're always going to use bash to run those scripts. Other shells may try to either emulate bash, or adhere to the POSIX standard, which bash supports pretty well (though adds its own extensions to).
It's possible to specify at the top of a shell script which shell it should be run with using a shebang. A script may specify #!/bin/bash
on the first line, meaning that the script should always be run with bash, rather than another shell.
/bin/sh is an executable representing the system shell. Actually, it is usually implemented as a symbolic link pointing to the executable for whichever shell is the system shell. The system shell is kind of the default shell that system scripts should use. In Linux distributions, for a long time this was usually a symbolic link to bash, so much so that it has become somewhat of a convention to always link /bin/sh to bash or a bash-compatible shell. However, in the last couple of years Debian (and Ubuntu) decided to switch the system shell from bash to dash - a similar shell - breaking with a long tradition in Linux (well, GNU) of using bash for /bin/sh. Dash is seen as a lighter, and much faster, shell which can be beneficial to boot speed (and other things that require a lot of shell scripts, like package installation scripts).
Dash is fairly well compatible with bash, being based on the same POSIX standard. However, it doesn't implement the bash-specific extensions. There are scripts in existence that use #!/bin/sh
(the system shell) as their shebang, but which require bash-specific extensions. This is currently considered a bug that should be fixed by Debian and Ubuntu, who require /bin/sh to be able to work when pointed to dash.
Even though Ubuntu's system shell is pointing to dash, your login shell as a user continues to be bash at this time. That is, when you log in to a terminal emulator anywhere in Linux, your login shell will be bash. Speed of operation is not so much a problem when the shell is used interactively, and users are familiar with bash (and may have bash-specific customisations in their home directory).
What you should use when writing scripts
If your script requires features only supported by bash, use #!/bin/bash
.
But if at all possible, it would be good to make sure your script is POSIX-compatible, and use #!/bin/sh
, which should always, quite reliably, point to the preferred POSIX-compatible system shell in any installation.
add a comment |
On Linux and other Unix-like systems you have a choice of multiple shells.
The shell is responsible not only for drawing your little prompt, but interpreting your commands, especially if you put in complicated logic like pipes, conditionals and so on.
bash is the most common shell used as a default shell for users of Linux systems. It is a spiritual descendent of other shells used throughout Unix history. Its name, bash is an abbreviation of Bourne-Again Shell, an homage to the Bourne shell it was designed to replace, though it also incorporates features from the C Shell and the Korn Shell.
It's run, these days, from /bin/bash
- any system with bash will have it accessible here.
It's not just users that use shells, though. Scripts (shell scripts) need shells to interpret them. When you run a shell script, your system needs to start up a shell process to execute your script.
The problem is, different shells have tiny little inconsistencies between them, and when it comes to running scripts, these can be a real problem. bash has quite a lot of scripting features that are unique only to bash, and not to other shells. This is fine, if you're always going to use bash to run those scripts. Other shells may try to either emulate bash, or adhere to the POSIX standard, which bash supports pretty well (though adds its own extensions to).
It's possible to specify at the top of a shell script which shell it should be run with using a shebang. A script may specify #!/bin/bash
on the first line, meaning that the script should always be run with bash, rather than another shell.
/bin/sh is an executable representing the system shell. Actually, it is usually implemented as a symbolic link pointing to the executable for whichever shell is the system shell. The system shell is kind of the default shell that system scripts should use. In Linux distributions, for a long time this was usually a symbolic link to bash, so much so that it has become somewhat of a convention to always link /bin/sh to bash or a bash-compatible shell. However, in the last couple of years Debian (and Ubuntu) decided to switch the system shell from bash to dash - a similar shell - breaking with a long tradition in Linux (well, GNU) of using bash for /bin/sh. Dash is seen as a lighter, and much faster, shell which can be beneficial to boot speed (and other things that require a lot of shell scripts, like package installation scripts).
Dash is fairly well compatible with bash, being based on the same POSIX standard. However, it doesn't implement the bash-specific extensions. There are scripts in existence that use #!/bin/sh
(the system shell) as their shebang, but which require bash-specific extensions. This is currently considered a bug that should be fixed by Debian and Ubuntu, who require /bin/sh to be able to work when pointed to dash.
Even though Ubuntu's system shell is pointing to dash, your login shell as a user continues to be bash at this time. That is, when you log in to a terminal emulator anywhere in Linux, your login shell will be bash. Speed of operation is not so much a problem when the shell is used interactively, and users are familiar with bash (and may have bash-specific customisations in their home directory).
What you should use when writing scripts
If your script requires features only supported by bash, use #!/bin/bash
.
But if at all possible, it would be good to make sure your script is POSIX-compatible, and use #!/bin/sh
, which should always, quite reliably, point to the preferred POSIX-compatible system shell in any installation.
add a comment |
On Linux and other Unix-like systems you have a choice of multiple shells.
The shell is responsible not only for drawing your little prompt, but interpreting your commands, especially if you put in complicated logic like pipes, conditionals and so on.
bash is the most common shell used as a default shell for users of Linux systems. It is a spiritual descendent of other shells used throughout Unix history. Its name, bash is an abbreviation of Bourne-Again Shell, an homage to the Bourne shell it was designed to replace, though it also incorporates features from the C Shell and the Korn Shell.
It's run, these days, from /bin/bash
- any system with bash will have it accessible here.
It's not just users that use shells, though. Scripts (shell scripts) need shells to interpret them. When you run a shell script, your system needs to start up a shell process to execute your script.
The problem is, different shells have tiny little inconsistencies between them, and when it comes to running scripts, these can be a real problem. bash has quite a lot of scripting features that are unique only to bash, and not to other shells. This is fine, if you're always going to use bash to run those scripts. Other shells may try to either emulate bash, or adhere to the POSIX standard, which bash supports pretty well (though adds its own extensions to).
It's possible to specify at the top of a shell script which shell it should be run with using a shebang. A script may specify #!/bin/bash
on the first line, meaning that the script should always be run with bash, rather than another shell.
/bin/sh is an executable representing the system shell. Actually, it is usually implemented as a symbolic link pointing to the executable for whichever shell is the system shell. The system shell is kind of the default shell that system scripts should use. In Linux distributions, for a long time this was usually a symbolic link to bash, so much so that it has become somewhat of a convention to always link /bin/sh to bash or a bash-compatible shell. However, in the last couple of years Debian (and Ubuntu) decided to switch the system shell from bash to dash - a similar shell - breaking with a long tradition in Linux (well, GNU) of using bash for /bin/sh. Dash is seen as a lighter, and much faster, shell which can be beneficial to boot speed (and other things that require a lot of shell scripts, like package installation scripts).
Dash is fairly well compatible with bash, being based on the same POSIX standard. However, it doesn't implement the bash-specific extensions. There are scripts in existence that use #!/bin/sh
(the system shell) as their shebang, but which require bash-specific extensions. This is currently considered a bug that should be fixed by Debian and Ubuntu, who require /bin/sh to be able to work when pointed to dash.
Even though Ubuntu's system shell is pointing to dash, your login shell as a user continues to be bash at this time. That is, when you log in to a terminal emulator anywhere in Linux, your login shell will be bash. Speed of operation is not so much a problem when the shell is used interactively, and users are familiar with bash (and may have bash-specific customisations in their home directory).
What you should use when writing scripts
If your script requires features only supported by bash, use #!/bin/bash
.
But if at all possible, it would be good to make sure your script is POSIX-compatible, and use #!/bin/sh
, which should always, quite reliably, point to the preferred POSIX-compatible system shell in any installation.
On Linux and other Unix-like systems you have a choice of multiple shells.
The shell is responsible not only for drawing your little prompt, but interpreting your commands, especially if you put in complicated logic like pipes, conditionals and so on.
bash is the most common shell used as a default shell for users of Linux systems. It is a spiritual descendent of other shells used throughout Unix history. Its name, bash is an abbreviation of Bourne-Again Shell, an homage to the Bourne shell it was designed to replace, though it also incorporates features from the C Shell and the Korn Shell.
It's run, these days, from /bin/bash
- any system with bash will have it accessible here.
It's not just users that use shells, though. Scripts (shell scripts) need shells to interpret them. When you run a shell script, your system needs to start up a shell process to execute your script.
The problem is, different shells have tiny little inconsistencies between them, and when it comes to running scripts, these can be a real problem. bash has quite a lot of scripting features that are unique only to bash, and not to other shells. This is fine, if you're always going to use bash to run those scripts. Other shells may try to either emulate bash, or adhere to the POSIX standard, which bash supports pretty well (though adds its own extensions to).
It's possible to specify at the top of a shell script which shell it should be run with using a shebang. A script may specify #!/bin/bash
on the first line, meaning that the script should always be run with bash, rather than another shell.
/bin/sh is an executable representing the system shell. Actually, it is usually implemented as a symbolic link pointing to the executable for whichever shell is the system shell. The system shell is kind of the default shell that system scripts should use. In Linux distributions, for a long time this was usually a symbolic link to bash, so much so that it has become somewhat of a convention to always link /bin/sh to bash or a bash-compatible shell. However, in the last couple of years Debian (and Ubuntu) decided to switch the system shell from bash to dash - a similar shell - breaking with a long tradition in Linux (well, GNU) of using bash for /bin/sh. Dash is seen as a lighter, and much faster, shell which can be beneficial to boot speed (and other things that require a lot of shell scripts, like package installation scripts).
Dash is fairly well compatible with bash, being based on the same POSIX standard. However, it doesn't implement the bash-specific extensions. There are scripts in existence that use #!/bin/sh
(the system shell) as their shebang, but which require bash-specific extensions. This is currently considered a bug that should be fixed by Debian and Ubuntu, who require /bin/sh to be able to work when pointed to dash.
Even though Ubuntu's system shell is pointing to dash, your login shell as a user continues to be bash at this time. That is, when you log in to a terminal emulator anywhere in Linux, your login shell will be bash. Speed of operation is not so much a problem when the shell is used interactively, and users are familiar with bash (and may have bash-specific customisations in their home directory).
What you should use when writing scripts
If your script requires features only supported by bash, use #!/bin/bash
.
But if at all possible, it would be good to make sure your script is POSIX-compatible, and use #!/bin/sh
, which should always, quite reliably, point to the preferred POSIX-compatible system shell in any installation.
edited Feb 4 '18 at 22:26
answered May 25 '12 at 8:31
thomasrutterthomasrutter
27.3k47089
27.3k47089
add a comment |
add a comment |
In addition to the previous answers, even if /bin/sh
is a symbolic link to /bin/bash
, #!/bin/sh
is not totally equivalent to #!/bin/bash
.
From the bash(1) man page :
"If bash is invoked with the name sh, it tries to mimic the startup
behavior of historical versions of sh as closely as possible, while
conforming to the POSIX standard as well."
For example the bash specific syntax :
exec > >(tee logfile.txt)
gives an error in a shell beginning with #!/bin/sh
, even with the sh->bash symbolic link in place.
add a comment |
In addition to the previous answers, even if /bin/sh
is a symbolic link to /bin/bash
, #!/bin/sh
is not totally equivalent to #!/bin/bash
.
From the bash(1) man page :
"If bash is invoked with the name sh, it tries to mimic the startup
behavior of historical versions of sh as closely as possible, while
conforming to the POSIX standard as well."
For example the bash specific syntax :
exec > >(tee logfile.txt)
gives an error in a shell beginning with #!/bin/sh
, even with the sh->bash symbolic link in place.
add a comment |
In addition to the previous answers, even if /bin/sh
is a symbolic link to /bin/bash
, #!/bin/sh
is not totally equivalent to #!/bin/bash
.
From the bash(1) man page :
"If bash is invoked with the name sh, it tries to mimic the startup
behavior of historical versions of sh as closely as possible, while
conforming to the POSIX standard as well."
For example the bash specific syntax :
exec > >(tee logfile.txt)
gives an error in a shell beginning with #!/bin/sh
, even with the sh->bash symbolic link in place.
In addition to the previous answers, even if /bin/sh
is a symbolic link to /bin/bash
, #!/bin/sh
is not totally equivalent to #!/bin/bash
.
From the bash(1) man page :
"If bash is invoked with the name sh, it tries to mimic the startup
behavior of historical versions of sh as closely as possible, while
conforming to the POSIX standard as well."
For example the bash specific syntax :
exec > >(tee logfile.txt)
gives an error in a shell beginning with #!/bin/sh
, even with the sh->bash symbolic link in place.
edited Jan 14 '15 at 11:27
muru
1
1
answered Jan 14 '15 at 11:24
AnkiFAnkiF
17112
17112
add a comment |
add a comment |
GNU Bash: #!/bin/bash
;
POSIX shell: #!/bin/sh
.
Well, short but accurate answer. 还行
– Sergiy Kolodyazhnyy
Dec 24 '18 at 11:11
add a comment |
GNU Bash: #!/bin/bash
;
POSIX shell: #!/bin/sh
.
Well, short but accurate answer. 还行
– Sergiy Kolodyazhnyy
Dec 24 '18 at 11:11
add a comment |
GNU Bash: #!/bin/bash
;
POSIX shell: #!/bin/sh
.
GNU Bash: #!/bin/bash
;
POSIX shell: #!/bin/sh
.
answered Dec 24 '18 at 7:27
山茶树和葡萄树山茶树和葡萄树
311
311
Well, short but accurate answer. 还行
– Sergiy Kolodyazhnyy
Dec 24 '18 at 11:11
add a comment |
Well, short but accurate answer. 还行
– Sergiy Kolodyazhnyy
Dec 24 '18 at 11:11
Well, short but accurate answer. 还行
– Sergiy Kolodyazhnyy
Dec 24 '18 at 11:11
Well, short but accurate answer. 还行
– Sergiy Kolodyazhnyy
Dec 24 '18 at 11:11
add a comment |
Thanks for contributing an answer to Ask Ubuntu!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f141928%2fwhat-is-the-difference-between-bin-sh-and-bin-bash%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown