Updated: Apr 17

Shell scripting: Parsing command-line arguments and flags easily

A Shell provides you with an interface to the Unix system. It gathers input from you and executes programs based on that input. When a program finishes executing, it displays that program's output.

Shell is an environment in which we can run our commands, programs, and shell scripts. There are different flavors of a shell, just as there are different flavors of operating systems. Each flavor of shell has its own set of recognized commands and functions.

Common command line:

list items in current directory


change directory


see name of current directory


see name of parent directory above


present open directory


change directory to current one

cd .

change directory to parent directory above

cd ..

or cd ../.. to move up two levels

make a new folder

mkdir <directoryname>

example: mkdir folder1

copy a file

cp file1 file2

Shell Bash Prompt

The prompt, $, which is called the command prompt, is issued by the shell. While the prompt is displayed, you can type a command.

Use Shell to communicate with the kernel.

Shell reads your input after you press Enter. It determines the command you want executed by looking at the first word of your input. A word is an unbroken set of characters. Spaces and tabs separate words.

Following is a simple example of the date command, which displays the current date and time −

Thu Jun 25 08:30:19 MST 2009

You can customize your command prompt using the environment variable PS1 explained in the Environment tutorial.

Shell Types

In Unix, there are two major types of shells −

  • Bourne shell − If you are using a Bourne-type shell, the $ character is the default prompt.

  • C shell − If you are using a C-type shell, the % character is the default prompt.

The Bourne Shell has the following subcategories −

  • Bourne shell (sh)

  • Korn shell (ksh)

  • Bourne Again shell (bash)

  • POSIX shell (sh)

The different C-type shells follow −

  • C shell (csh)

  • TENEX/TOPS C shell (tcsh)

The original Unix shell was written in the mid-1970s by Stephen R. Bourne while he was at the AT&T Bell Labs in New Jersey.

Bourne shell was the first shell to appear on Unix systems, thus it is referred to as "the shell".

Bourne shell is usually installed as /bin/sh on most versions of Unix. For this reason, it is the shell of choice for writing scripts that can be used on different versions of Unix.

In this chapter, we are going to cover most of the Shell concepts that are based on the Borne Shell.

Shell Scripts

The basic concept of a shell script is a list of commands, which are listed in the order of execution. A good shell script will have comments, preceded by # sign, describing the steps.

There are conditional tests, such as value A is greater than value B, loops allowing us to go through massive amounts of data, files to read and store data, and variables to read and store data, and the script may include functions.

We are going to write many scripts in the next sections. It would be a simple text file in which we would put all our commands and several other required constructs that tell the shell environment what to do and when to do it.

Shell scripts and functions are both interpreted. This means they are not compiled.

Example Script

Assume we create a script. Note all the scripts would have the .sh extension. Before you add anything else to your script, you need to alert the system that a shell script is being started. This is done using the shebang construct. For example −


This tells the system that the commands that follow are to be executed by the Bourne shell. It's called a shebang because the # symbol is called a hash, and the ! symbol is called a bang.

To create a script containing these commands, you put the shebang line first and then add the commands −


Useful commands

Files and Directories

These commands allow you to create directories and handle files.

Given below is the list of commands in Files and Directories.

Sr.No.Command & Description

cat Displays File Contents

cd Changes Directory to dirname

chgrp Changes file group

chmod Changes permissions

cp Copies source file into destination

file Determines file type

find Finds files

grep Searches files for regular expressions

head Displays first few lines of a file

ln Creates softlink on oldname

ls Displays information about file type

mkdir Creates a new directory dirname

more Displays data in paginated form

mv Moves (Renames) an oldname to newname

pwd Prints current working directory

m Removes (Deletes) filename

rmdir Deletes an existing directory provided it is empty

tail Prints last few lines in a file

touch Updates access and modification time of a file

Manipulating data

The contents of files can be compared and altered with the following commands.

Given below is the list of commands in Manipulating data.

Sr.No.Command & Description

awk Pattern scanning and processing language

cmp Compares the contents of two files

comm Compares sorted data

cut Cuts out selected fields of each line of a file

diff Differential file comparator

expand Expands tabs to spaces

join Joins files on some common field

perl Data manipulation language

sed Stream text editor

sort Sorts file data

split Splits file into smaller files

tr Translates characters

uniq Reports repeated lines in a file

wc Counts words, lines, and characters

vi Opens vi text editor

vim Opens vim text editor

fmt Simple text formatter

spell Checks text for spelling error

ispell Checks text for spelling error

emacs GNU project Emacs

ex, edit Line editor

emacs GNU project Emacs

Compressed Files

Files may be compressed to save space. Compressed files can be created and examined.


Compresses file


Helps uncompress gzipped files


GNU alternative compression method


Helps uncompress files


List, test and extract compressed files in a ZIP archive


Cat a compressed file


Compares compressed files


Compares compressed files


File perusal filter for crt viewing of compressed text

Getting Information

Various Unix manuals and documentation are available on-line. The following Shell commands give information −


Locates commands by keyword lookup


Displays command information pages online


Displays manual pages online


Searches the whatis database for complete words


GNOME help viewer

Network Communication

These following commands are used to send and receive files from a local Unix hosts to the remote host around the world.


File transfer program


Remote file copy


Remote login to a Unix host


Remote shell


Trivial file transfer program


Makes terminal connection to another host


Secures shell terminal or command connection


Secures shell remote file copy


Secures shell file transfer program

Some of these commands may be restricted at your computer for security reasons.

Programming Utilities

The following programming tools and languages are available based on what you have installed on your Unix.

Given below is the list of tools and languages in Programming Utilities.

Sr.No.Command & Description1

dbx Sun debugger2gdb GNU debugger3make Maintains program groups and compile programs4nm Prints program's name list5size Prints program's sizes6strip Removes symbol table and relocation bits7cb C program beautifier8cc ANSI C compiler for Suns SPARC systems9ctrace C program debugger10gcc GNU ANSI C Compiler11indent Indent and format C program source12bc Interactive arithmetic language processor13gcl GNU Common Lisp14perl General purpose language15php Web page embedded language16py Python language interpreter17asp Web page embedded language18CC C++ compiler for Suns SPARC systems19g++ GNU C++ Compiler20javac JAVA compiler21appletvieweir JAVA applet viewer22netbeans Java integrated development environment on Linux23sqlplus Runs the Oracle SQL interpreter24sqlldr Runs the Oracle SQL data loader25mysql Runs the mysql SQL interpreter

Misc Commands

These commands list or alter information about the system −

Given below is the list of Misc Commands in Unix.

Sr.No.Command & Description1

chfn Changes your finger information2chgrp Changes the group ownership of a file3chown Changes owner4date Prints the date5determin Automatically finds terminal type6du Prints amount of disk usage7echo Echo arguments to the standard options8exit Quits the system9finger Prints information about logged-in users10groupadd Creates a user group11groups Show group memberships12homequota Shows quota and file usage13iostat Reports I/O statistics14kill Sends a signal to a process15last Shows last logins of users16logout Logs off Unix17lun Lists user names or login ID18netstat Shows network status19passwd Changes user password20passwd Changes your login password21printenv Displays value of a shell variable22ps Displays the status of current processes23ps Prints process status statistics24quota -v Displays disk usage and limits25reset Resets terminal mode26script Keeps script of terminal session27script Saves the output of a command or process28setenv Sets environment variables30stty Sets terminal options31time Helps time a command32top Displays all system processes33tset Sets terminal mode34tty Prints current terminal name35umask Show the permissions that are given to view files by default36uname Displays name of the current system37uptime Gets the system up time38useradd Creates a user account39users Prints names of logged in users40vmstat Reports virtual memory statistics41w Shows what logged in users are doing42who Lists logged in users

Unix OS file system permissions

root is the permission on unix. Why wasn't I taught this at university? Why learning this on my own now??

A programmer can write a working program, while a computer scientist can analyze the solution and notice that changing some data structures or algorithms will yield a million-fold performance improvement. Or something.

In general CompSci is about analyzing and creating the tools used by programmers. Kind of how math is related to physics.

Programming can be self taught, *and* computer science can be self-taught -- but unless you realize that those are two separate things (and that you need both), you don't have the basic background necessary to even begin teaching yourself these things and would be better off having someone start you out.

I'm a big fan of autodidacticism, and I really don't believe that a degree is or should be necessary (or that it necessarily helps). But, I've worked with enough code written by non-professionals that I place a lot of value in professional programming habits and professional-level background knowledge.

A lot of this professionalism, unfortunately, isn't stuff that's typically taught in university CS programs either -- a lot of people aren't taught to use versioning systems, or aren't taught how to use them properly; a lot of people never pick up a unix tool until they go into industry and discover that they're expected to work with enterprise systems that you can't just walk up to and start visual studio on; a lot of universities have 'computer science' programs that are really just a series of tutorials on how to write a variety of simple java programs, and so the students there never learn actual CS topics and never really learn to code either.

So, even if you major in CS, you will need to learn a great deal on your own in order to be a competent programmer -- your degree program will not give you the skills or experience to prepare you for a real job! (Personal projects, if they are complex enough, will give you these skills if you let them. A lot of inexperienced programmers somehow manage to withstand the pressures of the problems they're trying to solve -- they do amazing things and then burn out completely because they've been writing everything in an IDE or in notepad, and making a copy of every file with a number after it or commenting out vast chunks of code rather than using a versioning system; they're using exactly the wrong tools for the job because the correct tool is prolog and they only know C++, or because the problem calls for mutexes and they don't know what a mutex is, or the problem calls for regexes and they've never heard of BNF.)

Learning actual CS at the same time as being in a CS degree program is a lot like double-majoring. But, there definitely are some overlaps. You might do even better if you learn CS at the same time as taking a degree program in, say, mathematics (particularly if you have an interest in logic programming or theorem proving or language formalisms), or physics (if you have an interest in simulations and games). But, in any case, you really need to have a good idea of what skills are genuinely necessary that aren't being taught by your university, because your university -- even if it's very good and has an extremely complete program -- won't teach you what you need to know for your first entry-level position during an undergraduate degree.

More useful commands for HyperCommand:


cd ..



touch //create a file

start // start text.txt file

rm // remove files

rm* // remove all files in a folder(destructive command, be careful in what directory you are)

rm -r <nameOfFolder/> / -r flag

Recent Posts

See All

Socket Programming in Python

Sockets and the socket API are used to send messages across a network. The network can be a logical, local network to the computer, or one that’s physically connected to an external network like the i

Financial Machine Learning

"The essential tool of econometrics is multivariate linear regression, an 18th century technology that was already mastered by Gauss before 1794...It is hard to believe that something as complex as 21

©2020 by Arturo Devesa.