Showing posts with label Shell Scripting. Show all posts
Showing posts with label Shell Scripting. Show all posts

Saturday, November 19, 2011

Shell Scripting 2 - What is Shell Scripting ?

In my previous post I've explained what Linux Shell is. Simply it is another program where you can give commands in text mode to get some work done.

However you can write a sequence of commands in a file and tell the shell to execute those commands. Then those files are called shell scripts. (File extension is .sh)

Such a script can be defined as a specific command defined to get a specific work done.

Shell scripting is very useful in a way that, if you want to do a task again and again then you can write the required commands as a shell script and execute it.

Ok, now let's try a simple shell script.

A Simple Shell Script

BASH is the default shell for Linux. Let's script using BASH commands. First we will write a simple script to print "Hello world ! " in the terminal.


You can use any text editor (gedit, vi, emacs, etc.) to write a shell script. Remember the script should be saved with .sh extension. 

Write the following lines in a file and save it as 'hello.sh'.
#!/bin/bash
echo "Hello, World!" 
Now you can execute this hello.sh script.
  1. Open a terminal
  2. Go to the location where you've saved the above file
  3. Then to execute, type, sh hello.sh (Or else you can give execution permission to the above file and then execute it.)
If you have a little touch with shell commands you know the above line no. 2 is enough for printing "Hello, World" in the terminal. So why do we include line no.1 's content ?

It is called a shebang or a "bang" line. In this line we give the absolute path to the BASH interpreter.

Shebang includes the hash sign and an exclamation point character (#!), followed by the full path to the interpreter such as /bin/bash. This will ensure that, the script is using the specified BASH interpreter even if it is executed under another shell.

Note : Giving execution permission to the script.

To give the execution permission to the file you can use the following command.

>> chmod  +x  /path to file

This is a very simple shell script. We can do a lot of things using shell scripting. Hope to bring them one by one in my next posts.

Friday, October 28, 2011

Shell Scripting 1 - What is Linux Shell ?

Do you know what Linux 'Kernel' is? Simply, it is the heart of the Linux Operating System. It manages the resources. (ie. Memory management, File management, I/O management, Device management, etc.) Kernel lies close to the hardware. So the language it can understand is more over low level. 

For humans to work with the operating system, there are human readable commands. Shell is the special program which converts those human readable commands in to the format which Kernel can understand. So shell is also a user program, which provides a more convenient environment for humans to interact with the system. 


Shell executes commands that are given from the keyboard or a file. There are several shells.
  • BASH
  • CSH
  • KSH
  • TCSH
You can access the Shell simply by opening a terminal. Or else you can connect through SSH (Secure Shell).

Note :  All the available shells in the system can be found in /etc/shells file.
            Current shell name is assigned to SHELL variable.

Let's see what is Shell Scripting in my next post.