The number one rule with Linux shell scripting is that there is no number one rule; and that's because there's no set language for programming a Linux script - it's just a matter of a programmer choosing a scripting language - one that will do the particular job, or one that the programmer is comfortable with - and then telling the system which script language interpreter is to be used.
Scripting Languages for Linux
There are numerous scripting languages for Linux - too many to cover here - and so this article will just give examples of four of them:
- Bash
- Perl
- PHP
- Python
Having chosen which language to use it's then essential that the script user should need to know nothing further about it - this means that they should be able to call it from the command line without knowing which interpreter is needed to run the script - and the key to doing this is the shebang line.
The Shebang Line
The shebang line is critical to any script - it's always the first line of the script and instructs the script which interpreter to use. It always has the same format:
- the shebang line starts with the symbols #! (hence the name)
- the shebang line ends with the full path to the interpreter
The location of the interpreter will vary according the Linux distribution being used, and the easiest way to find it is to use the which method:
So, for example, the first line of a Bash script would be:
A Example Bash Script
The first line of the script is obviously the shebang line - telling the system which interpreter to use; the script then:
- defines a function - in this case it simply formats the input to the function. One important thing to note about Bash functions is that they do not need input variables defined - they have in-built variables: $1 is the first input variable; $2 is the second; and so on
- the script then loops through any variables passed to the script - it uses the shift function to move variables to the left - in this way only the first variable needs to be checked.
The script can then be called from the command line:
An Example Perl Script
The Perl script is fundamentally the same as the Bash script - it just uses Perl syntax rather than Bash, and all of the input variables are included in an array - ARGV.
An Example PHP Script
Having seen the Bash and Perl scripts the PHP script is straightfoward - the only real difference is that as well as the argv array there is an argc variable - this contains a count of the number of input variables.
An Example Python Script
The Python script is similar to the Perl and PHP scripts - again it's just the format that is different - this time using indentation to create functions and command structures.
Conclusion
The key to the perfect Linux shell script is not necessarily which actual language is used by the script - that just comes down to the programmer's personal choice (or ability); the key to the perfect Linux shell script is the shebang line -by using this the programmer can ensure that the user needs know nothing about the language being used - the user just needs to know that the perfect Linux shell script actually exists.
Related Reading
Any Microsoft Windows programmers interested in scripting may like to read:
- Cygwin: a Linux-like Environment for Windows
- Microsoft Window's Big Secret: Scripting
And any Linux programmers may be interested in: