Shebang!

Many kinds of script files are found on a Linux system, and each interpreted language comes with a unique and specific command structure. There needs to be a way to tell Linux which interpreter to use for each script. This is accomplished by using a special line at the top of the script naming the appropriate interpreter. Linux examines this line and launches the specified interpreter program, which then reads the rest of the file. The special line must begin with #!, a construct often called shebang, often thought of as being short for Sharp (#) Bang (!). For bash, the shebang line is:

#!/bin/bash

This command explicitly states that the program named bash can be found in the /bin directory and designates bash to be the interpreter for the script. You’ll also see other types of lines on script files, including:

#!/bin/sh

The Bourne shell

#!/bin/csh

The C-shell

#!/bin/tcsh

The enhanced C-shell

#!/bin/sed

The stream editor

#!/usr/bin/awk

The awk programming language

#!/usr/bin/perl

The Perl programming language

Each of these lines specifies a unique command interpreter for the script lines that follow. (bash is fully backward-compatible with sh; sh is just a link to bash on Linux systems.). Note that the full paths given here are the default; some distributions might have slight differences. For example, Perl is often in /bin/perl or even /usr/local/bin/perl.

Get LPI Linux Certification in a Nutshell, 3rd Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.