Home » Linux » What is /bin/bash?

About Iulian Timis

Avatar photo
My name is Iulian and I graduated Faculty of Electronics, Telecommunications, and Information Technology. My master's degree was in Computer Science, Information Security. I am passionate about technology and the security area. During my career I was working as a Java Developer, on the backend and frontend side, sometimes doing some DevOps tasks.

What is /bin/bash?

1. Introduction

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: which bash.

The output of that command will be: /bin/bash. This defines the absolute path to the bash shell.

Example of using /bin/bash
Fig. 1: Example of using /bin/bash

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

/bin/bash:

  • 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.

/bin/sh:

  • 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: /usr/bin/env.

Running the env command will print you all the current environment variables defined by the shell.

Showing the output of the env command
Fig. 2: Output of the env command

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.

6. Conclusion

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.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

I have read and agree to the terms & conditions

 

Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments