Shell Scripting - Define #!/bin/bash
Last Updated :
04 Jan, 2022
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/bashDifference 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.
Similar Reads
Shell Scripting - Creating a Binary file While working in Linux systems, we have used so many commands on a day-to-day basis. Most of the commands are in the binary format resides under /bin, /sbin, /usr/bin, /usr/sbin, /usr/local/bin, etc directories. As system administrators, we would have to write many shell scripts to do a few tasks or
4 min read
Shell Scripting - Decision Making A Shell script is a plain text file. This file contains different commands for step-by-step execution. These commands can be written directly into the command line but from a re-usability perceptive it is useful to store all of the inter-related commands for a specific task in a single file. We can
7 min read
Bash Scripting - File Extension Bash scripting is a powerful tool for automating tasks and working with files in the command line. One important aspect of working with files in bash is understanding how to handle file extensions. In bash, a file extension is the portion of a file name that follows the last period (.) in the file n
6 min read
Shell Scripting - Set Command The `set` command in shell scripting is a powerful tool that used for controlling the behavior of the shell and the environment in which scripts run. It allows the users to modify the shell options and positional parameters which facilitates providing greater control over script execution and debugg
6 min read
Bash Script - Read User Input Interacting with users is a crucial aspect of many Bash scripts. Whether prompting for information, selecting options, or confirming actions, reading user input is essential for building powerful and flexible scripts. This guide provides a comprehensive overview of reading user input in Bash, coveri
8 min read