The jar Bar Basic Bash Scripting Guide by Yucca Nel

Here at The jar Bar , I have learnt and now make extensive use of Bash scripting . Most of my Linux tutorials examples and guides have Bash scripts and I even make use of Bash scripts to download and unpack things into Windows directories through means of VirtualBox ! . This is not a Bash tutorial , but just a brief explanation about Bash .

What is

The Gnu Projects Bourne Again Shell . A functional, loosely-typed scripting language for the modern Linux shell or terminal . Scripting languages allow you to dictate to the targeted entity. In the case of JavaScript, we target browsers. With Bash however, we target our system and tell it what to do.

A Bash Script is composed of:

  • A first line called the 'shebang' which usually looks like: #!bin/bash

  • Variables which may be loosely-typed

  • Functions which do not allow return values. Instead we can use the 'echo' command to give feedback to client code.

  • Loops, case control statements, if-else-then statements.

  • Ability to read input.

  • Logical operators.

Exiting a Bash Script

Too common an error in programming or scripting is the failure to exit correctly. It is imperative to exit with a correct code when scripting . A zero (0) indicates expected behaviour in the script or application , but an error should exit with a value of one (1) and an 'echo' message for debugging.

Naming a Bash Script

Simply give your plain text file or the script a meaningful name. It is not recommended to have any special characters in your file name as the shell tends to be very greedy with what gets entered and may mistake the character in a file name as a command! Bash scripts end with a 'sh' suffix e.g., myscript.sh

Running a Bash Script

Run the script by file name with the bash command e.g,
bash myscript.sh

Debugging your Bash Script

There are many possibilities but I find the easiest way to debug your Bash script is with the 'x' flag e.g.,
bash -x myscript.sh

Read up on Bash

When I get around to it, I will post tutorials about Bash I must stress though that it's not my priority as there is a wealth of information online and numerous good books which are available from Oreilly.com, Amazon.com or a multitude of online guides . I am learning Bash through help of Oreilly on a when-I-need-it basis. I personally find it very easy with a cookbook and simply referring to previous scripts . I my opinion this is the best way to learn it (through regular use).