Getting Started With the BASH Shell

Perhaps the first question you want to know when you are at the shell is where you are. The command pwd will give you your current working directory.

$ pwd

The ls command will give you a list of all files in the current directory.

$ ls

Giving it an argument, ls will determine if the argument is a directory. If it is, it will list the files contained in that directory. If it is not a file, ls will just list down the file as if echoing what you typed. However, if the file does not exist, ls will tell you that fact.

$ ls non-existent
non-existent not found

Some unix commands require a parameter in order to complete successfully. Some, like the ls command above, have default values. A unix command is structured in the following way:

command_name options arguments

options are usually preceded by a “-” sign. Options allow you have more control over the output of a command. For example, the command

$ ls -l

will list down detailed information about the files in the current directory. We will learn how to interpret the output of this command later when we gain more familiarity with the basic commands.

Exercise: List down the files of your parent directory.

Other basic commands.

To change to a directory, we use the cd command. It takes a single argument, which is the directory to change to. If you don’t specify an argument, it will change to your home directory, as defined by the HOME environment variable.

To create a directory, we use the mkdir command. It takes one or more arguments. These arguments are the names of the directories to create.

$ mkdir tmp1 tmp2 tmp3

will create directories tmp1, tmp2 tmp3 as shown by the ls command below.

$ ls -F

tmp1/ tmp2/ tmp3/

Question: What does the -F option to ls do?
We can remove the directories we just created using the rmdir command.

$ rmdir tmp1 tmp2 tmp3

If a directory is not empty, rmdir will refuse to remove that directory unless it is first emptied of all files.

Exercise: How do you force rmdir to remove a non-empty directory.

We can create empty files using the command “touch”.

$ touch me

Will create the file named “me” in the current working directory.

To remove this file, use the “rm” command.

$ rm me

We should use the rm command with caution because you cannot undelete the file you just deleted. You can tell rm to ask you for confirmation to delete the file using the “-i” commandline switch.

$ rm -i me
me: ? (y/n)

We now know how to do some basic commands for unix. But we are still not able to do anything useful. Next we want to do is to be able to view the content of a file. There are many ways to peek
at the contents of files. For short files that will fit the screen, we can use the “cat” command.

$ cat filename

This will dump on the screen the contents of file named filename. If the file is long and will not fit one
screenful, the contents will just flash quickly on the screen and you will just be able to view the end portion
of the file.

To view longer files, we can use the “more” command.

$ more filename

will let you view the file page by page. The more command is also known as a “pager” command because it let’s you view a file page by page. If a file is long, you can scroll the the next page using the space bar. The only problem with the “more” command is that you cannot go back to the previous page. You can only scroll-down, no up.

A more powerful pager than “more” is the “less” command. It allows you to scroll up and down. In this respect, we can say that “less” is more.

$ less long-file

To scroll backwards, press the “b” key.

Shell environment variables.

The shell is not only a command interpreter, it is also a programming environment. By an environment, what we mean is that is gives you the necessary tools to create programs conveniently and according to your own programming style and preference. We will explore that later.

As we have seen before, some commands are able to operate without an argument, like the cd command. The cd commands depends on the environment variable HOME which stores the value of your home directory. To view the value of this variable we use the echo command.

$ echo $HOME

Using this value, cd command will change to that directory when there are no arguments.

There are predefined environment variables when your account is first created. Many commands depend on the definitions of these variable for proper functioning. (just like the human body depends on some factors in order to operate properly). Among the most important variables you the shell depends are PATH. The path tells the shell where to find programs. Accidentally changing the value of this variable can give you a big headache as the shell cannot anymore find some programs.

You can also define your own environment variables. To define an environment variable, the syntax is


For example, to define the variable QUOTE to “Health is wealth.”
we type

$ QUOTE=”Health is wealth.”

Notice that the value is enclosed in quotation marks. This will tell the shell that the value contains spaces. If the quotation marks is ommitted an error will occur, like this:

$ QUOTE=Health is wealth
bash: is: command not found

As you can see, the shell interpreted “is” as a command, and having found none, it issues the error “command not found”.


Published by

Bobby Corpus

Loves to Compute!

Leave a Reply

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

You are commenting using your 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