From Linux Shell Scripting Tutorial - A Beginner's handbook
Jump to navigation Jump to search

← Hello, World! TutorialHomeShell Comments →

The #! syntax used in scripts to indicate an interpreter for execution under UNIX / Linux operating systems. Most Linux shell and perl / python script starts with the following line:









#!/usr/bin/env bash

Starting a Script With #!

  1. It is called a shebang or a "bang" line.
  2. It is nothing but the absolute path to the Bash interpreter.
  3. It consists of a number sign and an exclamation point character (#!), followed by the full path to the interpreter such as /bin/bash.
  4. All scripts under Linux execute using the interpreter specified on a first line[1].
  5. Almost all bash scripts often begin with #!/bin/bash (assuming that Bash has been installed in /bin)
  6. This ensures that Bash will be used to interpret the script, even if it is executed under another shell[2].
  7. The shebang was introduced by Dennis Ritchie between Version 7 Unix and 8 at Bell Laboratories. It was then also added to the BSD line at Berkeley [3].

Ignoring An Interpreter Line (shebang)

  • If you do not specify an interpreter line, the default is usually the /bin/sh. But, it is recommended that you set #!/bin/bash line.


For a system boot script, use /bin/sh:


sh is the standard command interpreter for the system. The current version of sh is in the process of being changed to conform with the POSIX 1003.2 and 1003.2a specifications for the shell.

An example of /bin/sh script

  • /etc/init.d/policykit
 1 #! /bin/sh
 3 # Provides:          policykit
 4 # Required-Start:    $local_fs
 5 # Required-Stop:     $local_fs
 6 # Default-Start:     2 3 4 5
 7 # Default-Stop:
 8 # Short-Description: Create PolicyKit runtime directories
 9 # Description:       Create directories which PolicyKit needs at runtime,
10 #                    such as /var/run/PolicyKit
13 # Author: Martin Pitt <>
15 case "$1" in
16   start)
17         mkdir -p /var/run/PolicyKit
18         chown root:polkituser /var/run/PolicyKit
19         chmod 770 /var/run/PolicyKit
20 	;;
21   stop|restart|force-reload)
22 	;;
23   *)
24 	echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
25 	exit 3
26 	;;
27 esac
29 :

For a typical script use the #!/bin/bash shell.

/usr/bin/env bash

The /usr/bin/env run a program such as a bash in a modified environment. It makes your bash script portable. The advantage of #!/usr/bin/env bash is that it will use whatever bash executable appears first in the running user's $PATH variable.

#!/usr/bin/env bash
# Purpose: Mount glusterfs at boot time
#          Must run as root
# Author: Vivek Gite
# --------------------------------------

mount | grep -wq "^${p}"

if [ $? -ne 0 ]
	mount -t glusterfs "$p" /sharedwww/

External links


  1. Howto Make Script More Portable With #!/usr/bin/env As a Shebang FAQ by nixCraft.
  2. Bash man page and the official documentation.
  3. extracts from 4.0BSD /usr/src/sys/newsys/sys1.c.

← Hello, World! TutorialHomeShell Comments →