Unix Shell Scripting

22 Jan

Shell Scripting Introduction:

Unix uses shells to accept commands given by the user, there are quite a few different shells available. The most commonly used
shells are SH(Bourne SHell) CSH(C SHell) and KSH(Korn SHell), most of the other shells you encounter will be variants of these
shells and will share the same syntax, KSH is based on SH and so is BASH(Bourne again shell). TCSH(Extended C SHell) is
based on CSH.
The various shells all have built in functions which allow for the creation of shell scripts, that is, the stringing together of shell
commands and constructs to automate what can be automated in order to make life easier for the user.

One is not required to use a specific shell in order to exploit its features because we can specify the shell we want to interpret our shell script within the script itself by including the following in the first line.

Note:Usually anything following (#) is interpreted as a comment and ignored but if it occurs on the first line with a (!) following it is treated as being special and the filename following the (!) is considered to point to the location of the shell that should interpret the script.

Basic commands in Unix:

  • ls — lists your files
    ls -l — lists your files in ‘long format’, which contains lots of useful information, e.g. the exact size of the file, who owns the file and who has the right to look at it, and when it was last modified.
    ls -a — lists all files, including the ones whose filenames begin in a dot, which you do not always want to see.
  • more filename — shows the first part of a file, just as much as will fit on one screen. Just hit the space bar to see more or q to quit. You can use /pattern to search for a pattern.
  • mv filename1 filename2 — moves a file (i.e. gives it a different name, or moves it into a different directory
  • cp filename1 filename2 — copies a file
  • rm filename — removes a file
  • diff filename1 filename2 — compares files, and shows where they differ
  • wc filename — tells you how many lines, words, and characters there are in a file
  • chmod options filename — lets you change the read, write, and execute permissions on your files. The default is that only you can look at them and change them, but you may sometimes want to change these permissions. For example, chmod o+r filename will make the file readable for everyone, and chmod o-r filename will make it unreadable for others again

File Compression :

  • gzip filename — compresses files, so that they take up much less space
  • gunzip filename — uncompresses files compressed by gzip
  • gzcat filename — lets you look at a gzipped file without actually having to gunzip it


  • mkdir dirname — make a new directory
  • cd dirname — change directory
  • pwd — tells you where you currently are

Finding Things:

  • grep string filename(s) — looks for the string in the filesThis can be useful a lot of purposes, e.g. finding the right file among many, figuring out which is the right version of something, and even doing serious corpus work. grep comes in several varieties (grep, egrep, and fgrep) and has a lot of very flexible options.

Connecting to the outside world:

  • rlogin hostname — lets you connect to a remote host
  • telnet hostname — also lets you connect to a remote host.
  • ftp hostname — lets you download files from a remote host which is set up as an ftp-server. The most important commands within ftp are get for getting files from the remote machine, and put for putting them there (mget and mput let you specify more than one file at once).

You can find out more about these commands by looking up their manpages:
man commandname — shows you the manual page for the command

  • We use > to redirect stdout to a file, for instance, if we wanted to redirect a directory listing generated by the ls we could do the following:
    ls > file
  • We use < to specify that we want the command immediately before the redirection symbol to get its input from the source specified immediately after the symbol, for instance, we could redirect the input to grep(which searches for strings within files) so that it comes from a file like this:
    grep searchterm < file
  • We use >> to append stdout to a file, for instance, if we wanted to append the date to the end of a file we could redirect the output from date like so:
    date >> file

Pipelines are another form of redirection that are used to chain commands so that powerful composite commands can be constructed, the pipe symbol ‘|’ takes the stdout from the command preceding it and redirects it to the command following it.

eg. ls -ltr  | grep searchword | sort

The example above firsts requests a long (-l directory listing of the current directory using the ls command, the output from this is then piped to grep which filters out all the listings containing the searchword and then finally pipes this through to sort.


When a script starts all environment variables are turned into shell variables. New variables can be instantiated like this:


Variables are referenced like this: $name, here is an example:

eg1. #!/bin/sh
echo $msg1 $msg2
This would echo “Unix Scripts” to the console display..

Note: If a string has to be assigned to a variable and the string  contains spaces , string must be enclosed within double quotes(“).One can still use $ within a (“) quoted string to include variables.

eg2. #!/bin/sh

msg 1=”I”;

msg2=”$msg1 LOVE”

msg3=”$msg2 UNIX”

echo $msg3

Output: “I LOVE UNIX”


One Response to “Unix Shell Scripting”

  1. ruining code January 22, 2011 at 8:18 pm #

    Yogesh ,.. please find the output

    echo “Nice Introduction”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: