How to Create, Save and Run your Own BASH Scripts


Bash, an acronym for Bourne-again Shell, is a Unix shell written for the GNU Project and is the default shell for Linux and Mac OS X. The beauty of Bash scripts is that they let you accomplish a group of tasks quickly and without much intervention from the user like batch files in Windows.

The syntax is similar to structured programming languages like C, and will be easier to understand for people with prior knowledge of any such programming language. I assume that you are familiar with GNU/Linux commands and with the fundamental concepts of programming.

 

Writing, Saving And Running Your Own BASH Scripts

You can use any text editor to write or create your Bash scripts. Any Unix-based OS you work on should have Vim installed.

Save the scripts with the .sh extension anywhere on your computer. After saving it, open the terminal (as root, if necessary) and go to the folder where you saved the file.

To run your script, type bash yourscriptname.sh in the terminal prompt.

 

A Very Simple Script

The first thing that anyone new to programming learns is how to print “Hello World” on the screen:

echo Hello World

This is a one line script where the action performed by the script is to print “Hello World” on the computer screen. I will be writing more on BASH scripts in the future.

Filed under: Linux
Tags: , , , , ,
March 23, 2009 by: Prasanth Chandra

Leave a Reply