Writing You First Shell Script

Hi Readers,

Writing Shell Script is not difficult. Our first script will print "Knowledge is Power" on screen. To write shell script you can use in of the Linux's text editor such as vi or mcedit or even you can use cat command. Here we are using cat command. You can use any of the above text editor. First type following cat command
and rest of text as its

$ cat > first
#
# My first shell script
#
clear
echo "Knowledge is Power"


Press Ctrl + D to save. Now our script is ready. To execute it type command

$ ./first
This will give error since we have not set Execute permission for our script first; to do this type command.

$ chmod +x first

$ ./first

First screen will be clear, then Knowledge is Power is printed on screen.

How to Run Shell Scripts

Because of security of files, in Linux, the creator of Shell Script does not get execution permission by default. So if we wish to run shell script we have to do two things as follows

(1) Use chmod command as follows to give execution permission to our script

Syntax: chmod +x shell-script-name OR Syntax: chmod 777 shell-script-name

(2) Run our script as

Syntax: ./your-shell-program-name

For e.g.
$ ./first

Here '.'(dot) is command, and used in conjunction with shell script. The dot(.) indicates to current shell that the command following the dot(.) has to be executed in the same shell i.e. without the loading of another shell in memory.

Below is the sample shell script :

#!/bin/bash
#
# Title : Shell Script001 : Basic Script
# Author : Gaurav Khanna
#

#Clear Console
clear

#Print the statement within quotes
echo "Knowledge is Power"

# To Run Script, follow below steps :
#1. $ chmod +x scriptName
#2. $./scriptName