How to write a Linux Script (Shebang)?

Card Puncher Data Processing

About

This page is about the creation of Script in Linux.

They

  • can be written in many languages
  • does not need an extension
  • must be given the execution permission

Syntax (Shebang)

#!/bin/bash 
or
#!/usr/bin/env perl

where the first line called the shebang define the interpreter to run.

  • the # is the command
  • the exclamation mark (“!”) is affectionately called “bang”
  • /bin/bash the path to the interpreter.
  • env
    • start a new script without environment variable or
    • used when the script’s interpreter is in a non-standard location

The interpreter is not limited to bash. Scripts can be written in an other language such as:

  • When a script’s interpreter is in a non-standard location, it's recommended to use env
#!/usr/bin/env bash
#!/bin/csh
  • Python,
#!/bin/python
  • Perl
#!/bin/perl
#!/usr/bin/Rscript
  • Ruby, PHP, etc, etc.

The shebang is a kernel required syntax (not mandatory from the shell prespective)

Location

According to the FHS, the script may be located:

  • in the /bin/ or /usr/bin directory for use by all users
  • in the user HOME/bin otherwise





Discover More
Card Puncher Data Processing
Shell Data Processing - Sed (Stream editor)

sed stands for stream editor. It is a filter program used for filtering and transforming text It: takes as input a standard stream input modifies it based on one or more command, and returns...



Share this page:
Follow us:
Task Runner