Open In App

Shell Scripting – Define #!/bin/bash

Improve
Improve
Like Article
Like
Save
Share
Report

A shell provides an interface to connect with the system. When we use an operating system, we indirectly interact with the shell. While using a terminal every time on any Linux distribution system, we interact with the shell. The main function of the shell is to interpret or analyze Unix commands. A shell takes commands from the user and translates them into the kernel’s understandable form. In other words, it acts as a medium between the user and the kernel of the operating system. The kernel is a computer program that is considered as the main part of a computer’s operating system.

This article focuses upon the significance of #!/bin/bash on the top of a script.

Significance of #!/bin/bash:

This is known as shebang in Unix. Shebang is a collection of characters or letters that consist of a number sign and exclamation mark, that is (#!) at the beginning of a script. As discussed in the introduction part, the main function of a shell is to interpret the UNIX commands given by the user. Unix consists of numerous shells out of that bash is one of them that is widely used. It is the default shell assigned by Linux-based operating systems. 

A script comprises several UNIX commands. Now, in each of the scripts, users are required to explicitly specify the type of shell they want to use to run their scripts. Now to explicitly specify the type of shell used by the script, Shebang is used. So we can use shebang, that is, #!/bin/bash at the start or top of the script to instruct our system to use bash as a default shell.

Let us consider it with the help of an example. A script is nothing but several commands. In the below example, the first line of the script starts with a hash (#) and an exclamation mark (!).  It basically instructs the operating system to use Bash as a default shell to run the script. In the second line, we are printing “Hello World!” to the console.

Example:

// Using shebang to specify the OS to
// use bash shell
// myScript.sh

#!/bin/bash  
# Bash script  
echo "Hello World!"  

Output:

Significance of #!/bin/bash

Difference between #!/bin/bash and #!/bin/sh:

The shebang, #!/bin/bash when used in scripts is used to instruct the operating system to use bash as a command interpreter. Each of the systems has its own shells which the system will use to execute its own system scripts. This system shell can vary from OS to OS(most of the time it will be bash). Whereas, when the shebang, #!/bin/sh used in scripts instructs the internal system shell to start interpreting scripts.

Below are some of the shebangs used for different purposes in shell scripts:

  • #!/bin/sh: It is used to execute the file using sh, which is a Bourne shell, or a compatible shell
  • #!/bin/csh: It is used to execute the file using csh, the C shell, or a compatible shell.
  • #!/usr/bin/perl -T: It is used to execute using Perl with the option for taint checks.
  • #!/usr/bin/php: It is used to execute the file using the PHP command-line interpreter.
  • #!/usr/bin/python -O: It is used to execute using Python with optimizations to code.
  • #!/usr/bin/ruby: It is used to execute using Ruby.

Last Updated : 04 Jan, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads