Difference between sh and Bash

Introduction

When writing shell programs, we often use /bin/sh and /bin/bash. Many people are confused about the difference between these two. In this article, we will discuss the main differences between Bash and sh and what we need to be aware of when programming in Bash and sh.

Difference between Bash and sh

Bash (Bourne Again Shell) and sh (Bourne Shell) are two different types of shell programs available in Unix-based systems. Here are the main differences between them:

  • Features: Bash is an extended version of sh with additional features and improvements. It provides advanced features like command-line editing, command history, and programmable completion.
  • Compatibility: Sh is the original Unix shell and is available on all Unix-based systems. Bash is backward compatible with sh, which means that most sh scripts can be run using Bash.
  • Syntax: Bash has a more advanced syntax compared to sh. It supports additional constructs like brace expansion, command substitution, and arithmetic operations.
  • Interactive Mode: Bash provides an interactive mode with a prompt, where users can enter commands and get immediate feedback. Sh does not have an interactive mode.
  • Script Execution: Bash is usually used for executing scripts, while sh is often used for system boot scripts and simple, portable scripts.

Programming in Bash and sh

When programming in Bash and sh, there are a few things you need to be aware of:

  • Shell Script Shebang: The shebang at the beginning of a shell script specifies which shell program should be used to interpret the script. To use Bash as the interpreter, you can use #!/bin/bash, and to use sh, you can use #!/bin/sh. It is recommended to use #!/bin/bash if you need the advanced features of Bash.
  • Portability: If you want your shell script to be portable across different Unix-based systems, it is best to write it in sh. By using the sh syntax and avoiding Bash-specific features, your script will run on any system that has sh installed.
  • Bash-specific Features: If you need to use Bash-specific features in your script, make sure to use #!/bin/bash as the shebang and document it accordingly. This way, users who don't have Bash installed will be aware that the script requires Bash.
  • Testing Compatibility: If you are not sure whether your script will work correctly with sh, you can test it by running it with sh explicitly. You can do this by using sh script.sh instead of ./script.sh. This will ensure that the script is interpreted by sh, even if the shebang specifies Bash.

Conclusion

In summary, Bash and sh are two different shell programs with distinct features and syntax. Bash is an extended version of sh, providing additional features and improvements. When programming in Bash and sh, it is important to consider the compatibility and portability of your scripts. By understanding the differences between them, you can make an informed choice about which shell program to use based on your requirements.