When you type commands on a computer, you’re talking to the shell. It’s like a translator between you and the computer’s brain. Two main types of shells are the Bourne Shell (SH) and the Bourne Again Shell (Bash), each with its own way of understanding and doing things. Let’s explore the difference between SH and Bash and how they help us work with computers.
sh
#!/bin/sh
bash
#!/bin/bash
What are SH and Bash?
SH, the original Bourne Shell, was introduced in the 1970s as the primary shell for Unix. Stephen Bourne at Bell Labs developed it, hence the name. SH is known for its simplicity and portability. It is a reliable option when creating scripts that might be executed on various Unix derivatives.
Bash, on the other hand, stands for Bourne Again SHell. It’s an improved version of the original SH, developed by Brian Fox for the GNU Project as a free software replacement for the Bourne Shell. Bash is the default shell for many Linux distributions and macOS. It includes features from other shells like KornShell (ksh) and C shell (csh), offering more robust functionality and scripting capabilities.
Key Differences Between SH and Bash
Syntax Differences:
Bash provides more shorthand and enhanced scripting syntax compared to SH. For example, in bash, you can use the double square bracket syntax [[ ]]
for tests, which is more robust and safer than the single bracket syntax [ ]
used in SH.
# SH Syntax
if [ $a -lt $b ]; then
echo "$a is less than $b"
fi
# Bash Syntax
if [[ $a -lt $b ]]; then
echo "$a is less than $b"
fi
Array Variables:
Bash supports array variables, whereas SH does not. Arrays can be useful when you need to work with multiple values stored under a single variable name.
# Bash Syntax
array=("one" "two" "three")
echo ${array[1]} # Outputs "two"
Command Line Editing:
Bash supports command line editing, whereas this is not standard in SH. In Bash, you can navigate the command history using arrow keys, delete text with backspace, and use tab completion to complete file names and command names.
Shell Options:
Bash comes with additional shell options compared to SH. For instance, the shopt
command in Bash provides the ability to change additional shell optional behavior which is not available in SH.
Process Substitution:
Bash supports process substitution (<(command)
and >(command)
), allowing a process’s input or output to be referred to using a filename. This feature is not available in SH.
# Bash Syntax
diff <(command1) <(command2)
Command not found Hook:
Bash has a special shell function command_not_found_handle
that is executed when a command is not found. This feature is not available in SH.
Here Strings:
Bash supports “Here Strings” which is a form of I/O redirection allowing a string to be used as the input for a command, denoted by <<<
. This feature is not available in SH.
# Bash Syntax
wc -l <<< "This is a string"
Note
- A shell is an interface between the end-users and the operating systems.
- sh implements the shell command line interpreter.
- bash is a superset of sh.
In line with our commitment to enhancing your skills, we’re thrilled to announce the launch of our new blog series, “Mastering Bash Scripting for Embedded Linux Development”. This series aims to equip you with the knowledge and skills needed to excel in Bash scripting, particularly in the context of embedded Linux development.
Stay tuned as we delve deeper into the intricacies of Bash scripting, providing you with valuable insights and practical tips along the way. Happy scripting!
You can find the series:
1) Mastering Bash Scripting for Embedded Linux Development
[…] Difference Between sh and Bash in Linux? […]