What is /bin/bash?
Bash is known also as a Unix Shell, and was written by Brian Fox for the GNU Project as a free software replacement for the Bourne shell. The first version was released in 1989 and it was used as a default login shell for most Unix distributions. Currently, the bash is available on Windows 10 via Windows Subsystem for Linux. On all versions of Apple macOS is set as default, prior to the 2019 release of macOS Catalina, the default was replaced with zsh. Bash remains available but as an alternative shell.
When writing scripting on the Unix operating system you will need to know the bash scripting language. Bash script or also known as shell script is a computer program where the main objective was to be run by the Unix Shell, a command-line interpreter. Some common operations executed by the shell scripts are: file manipulation, program execution, and printing text. This stands for scripts running in automated mode and can vary from one operating system to another. Each operating system uses particular names for scripting: batch file (MSDos-Win95 stream, OS/2), command procedures (VMS), and shell scripts (Windows NT).
More information about shell scripts can be found here.
2. Name of the line #!/bin/bash
This is the first line from bash scripts. The name of this line is: she-bang‘(shabang), hashbang. Basically, this derives from the concatenation of two tokens, sharp(#) and bang (!). This combination of tokens will be present at the beginning of a script. It will specify which specific interpreter should be used to execute commands.
3. Usage of /bin/bash
This is the most common shell and most developers use it. It is the default shell for user login in the Unix system. Bash can execute the vast majority of scripts. Most developers use it because it has more features and has a better syntax.
To find out where the bash is located you can run from your command line this:
The output of that command will be:
/bin/bash. This defines the absolute path to the bash shell.
The usage of bash like other CLI-designed computer applications requires precision when working with files and data (large numbers of files or large quantities of data). Some of the common use cases for bash are:
- System administrators (use systems systematically and reproducibly).
- Software developers (used to automate some tasks such as code compilation, debugging source code, and others).
- Network engineer (to test/configure and optimize network performance).
- Computer science research (manage research systems and carry out research on those systems).
4. Comparison between /bin/bash and /bin/sh
We already got familiar with
/bin/bash shebang line, now let’s also say a few words about /bin/sh (Shell Command Language). This is a language defined by the POSIX Standard. Basically, bash and sh are two different shells. Bash is the same as sh but with more features and better syntax. It is implemented as a symbolic link/hard link pointing to the executable for whichever shell is the system shell. To find the symbol link/hard link you can run this:
file -h /bin/sh #finding the symbolic link OUTPUT: /bin/sh: symbolic link to dash find -L /bin -samefile /bin/sh #finding hard link for a file OUTPUT: /bin/sh.distrib /bin/sh /bin/dash
- It is the most common shell used as a default shell for users of Linux systems
- Make programming more convenient and similar to other modern programming languages
- Process substitution, Scoping variables, arrays.
- Many special variables such as $RANDOM, $SECONDS, etc.
- It is an executable representing the system shell
- Have standardization for this language
- It is much easy to learn
- Portable across POSIX systems
5. Usage of /usr/bin/env bash
As we mentioned before this line #!/usr/bin/env bash is called shebang. This will help us to execute commands with the defined interpreter. With that command will use the env variable to extract the environment variables, and after that execute the given command with that interpreter. The env from the shebang will instruct the system to look for the specified interpreter in the $PATH variable and get the first occurrence. The default location for Unix-based systems is:
/usr/bin/env. To verify its location you can type:
which env which will return you this output:
env command will print you all the current environment variables defined by the shell.
Comparing this line: #!/usr/bin/env bash with the classic one: #!/bin/bash we can say the followings:
- #!/usr/bin/env bash is more portable but has less security compared to the classic one.
- It will need to search in the env for the interpreter compared with the classic one where the first line is more specific.
- The classic version can support extra parameters passed after the declaration of the interpreter in contrast with the new one where it will not allow it because the system reads them as a single command.
Before writing scripts, it is good to know about the language itself, covering also a bit of history. In this article, we cover the concept of bash scripting, history, and the name of the line that should be added in the scripts for the interpreter to be able to parse it.
Besides that, we also describe the usage of /bin/bash, the comparison between /bin/bash and /bin/sh, and also the usage of /bin/env.
To sum up, in this article we present the basic concepts of bash scripting.