BASH Programming Basics

'''

BASH Shell Programming Basics
'''

Introduction
BASH is a type of UNIX shell which stands for Bourne Again SHell, named after it's author.


 * UNIX is a free Operating System which as a basis to Linux is becoming popular lately.


 * A shell is the command line interface which allows for user input to control programs which operate various functions of the computer.

Why Would I want to program in BASH?

 * 1) BASH is available on most installations of Unix and Linux
 * 2) BASH is free to use
 * 3) BASH can be used to control and provide input and output of Linux Applications through the use of scripts

Basic Script Creation

 * This guide assumes you are familiar with some aspects of text editors and Linux or UNIX. A text editor like Emacs, Vi, or Pico is used to create an ASCII text file.


 * If not please read Using Vi


 * Scripts which use BASH must have Linux execute permissions.


 * The easiest way to make sure you have execute permissions is to create a blank text file using vi and then issuing the command chmod 700 myscript, with myscript being the name of the file you created.

After Initial Creation
Steps to make a BASH Script


 * The BASH script needs the line #!/usr/bin/bash or its equivalent.
 * If the path to BASH on your machine is different you can find it by using the command which bash
 * You can use the number sign (#) to add a comment to make your program readable.
 * Eg. #This is a comment, the program will not read this line
 * Create a variable by directly assigning it
 * mytext="hello world"
 * mynumber=27
 * Use echo or another built in command to create output
 * echo displays on the screen what follows it.
 * To use a variable, put the $ in front of it to use its value once it has already been assigned a value.
 * echo "$mytext" will display "hello world" to the user
 * echo "$mynumber is my favorite number" will display "27 is my favorite number"
 * echo "mynumber" will display only "mynumber"

Your first "Hello World" BASH program
Using the above techniques you are now able to create your first BASH script.


 * Putting together the examples above you have:


 * #!/usr/bin/bash
 * #This is a comment
 * mytext="hello world"
 * echo "$mytext"


 * Use ./program_name to execute the program


 * Its output is "hello world"