Subscribe to our RSS Feed
feed image
Bash Shell Basics - Level 1

This tutorial is aimed at the individual who has just laid eyes on the Bash shell for the first time, and wants to know what it is and what it does. Many people, most of them Windows users, have never seen a real shell (and that worthless "Command Prompt" in Windows is, sorry to say, not a real shell). A good command line should allow you to perform just about every single task on the system, easily and quickly, without the need for a GUI. Don't get me wrong, I do believe that GUI's are very useful, and they make life a lot easier, but I also don't believe that the GUI is the most efficient way to do things, most of the time. So let's begin exploring the command line. If you think you know enough about the shell to move on to the commands, click here.

Introduction

In Linux, the command line is accessed by means of something called a shell. What is a shell? A shell can be envisioned as sort of an environment, all on its own, and separate from anything GUI-related. In other words, if you are reading this webpage from your graphical web browser (such as Firefox or IE), then you are currently in a graphical environment, complete with desktop icons, shortcut buttons on your taskbar, and graphical programs.

When you enter a shell, you will be in a textual environment, and thus will not see any graphical icons, graphical shortcuts, or graphical programs.

Many people may be thinking, "Well why would that be useful?" Well, let me tell you, while the shell may seem more complicated (only because you're so used to clicking your mouse to get everything done), it is worlds more capable and flexible than any graphical interface, for most situations.

In many popular Linux distributions (such as Ubuntu and Fedora), the preferred shell of choice is the Bash shell. Wait, there's more than one? Absolutely. In the Linux world, even the shell has many different versions and variants, and everyone has their own personal preference. There's the Bourne Shell, Korn Shell, C Shell, and Bourne Again Shell (or Bash), just to name a few (yes, there are many more).

To be honest, the Bash shell is my personal favorite, and the one I use the most. For me, it has very useful features to get things done quickly and prevent repetitive work (and I love that). If there's one thing I hate, it's typing a long command and hitting Enter only to see it fail, and then having to type the whole thing all over again. For people who type over 100 words per minute, it's probably not so much of an issue, but I can guarantee that someone who only types about 30 words per minute will get annoyed very, very quickly if that were to happen to them.

So anyway, enough babble, let's get our hands dirty with the Bash shell!

The Shell Environment

I'm going to assume that you're using Ubuntu for this tutorial (even if you're not, you should still be able to follow along). Go to Applications --> Accessories --> Terminal, to open your terminal program in Ubuntu. A terminal is simply a graphical program that gives you access to the shell (because remember that the shell is not graphical, so because you are already present in a graphical environment, you need some sort of medium to access a textual environment from within your graphical one).

Once you have opened the terminal, you should be staring at a Bash prompt. A prompt is a set of characters that is displayed in the shell as sort of a placeholder for where you can begin typing. The prompt is completely customizable, though by default the Bash shell's prompt will typically display your username, your computer's hostname, and even the directory where you are currently located. Below is a good example of the terminal, the shell displayed in the terminal, and a prompt:

Terminal

As you can see above, my prompt is displaying my username (joe), my computer's hostname (joelinux), and my current directory (~). The ~ symbol represents my home directory, which is where I will always be located the moment I first enter the shell. If you look at the image again, you will see a black box sitting to the right of my prompt. This black box is called the cursor. You may be familiar with this term, especially if you are used to using a word processor (such as Microsoft Word). In a word processor, when you begin typing, letters will appear wherever the cursor is located. The same concept applies to the shell. Wherever the cursor is focused is where your text will go.

You can open many terminals, and as a result you will be introduced to many separate instances of the shell. Each instance of the shell will have its own environment variables. You can think of environment variables as "virtual placeholders", and each of them hold information that is useful to the shell, and typically invisible to the user. For example, one particularly important environment variable is the HOME variable. This variable contains the location of your home directory (for my user account "joe", it is specifically "/home/joe"), which is important for the shell to know (after all, how can you be placed into your home directory if the shell doesn't know where your home directory is?). Each instance of the shell will have its own environment variables, and you can also modify them and add your own if you wish. You will see the usefulness of this in a future tutorial. For now, just know that they exist.

Specifying Paths

One more important thing to know before we begin using some commands: How to specify locations (either files or directories) on the system. What I mean, is that if you want to do something that needs to specify a location (maybe you want to move to another directory), there are two ways that you can tell the system where you want to go: Absolute path, and relative path. Don't get freaked, each concept is quite simple.

To specify an absolute path is to tell the shell the entire path of the file or directory, including all parent directories. Let me give you an example. Let's say I am located in my home directory, /home/joe. That directory contains three files, File_A, File_B, and File_C. I want to tell the shell that I am going to edit File_B. If I were to give the shell the absolute path, I would tell the shell that I wanted to edit /home/joe/File_B. In this case, I have specified the file's name (File_B), as well as its parent directory (joe), and every parent directory before that (home). Directories are of course separated by the forward-slash character (/).

To specify a relative path is to tell the shell only the part of the path of the file or directory that is relative to where you are located. Using the example in the previous paragraph, let's say I wanted to tell the shell that I am going to edit File_B. If I were to give the shell the relative path, I would tell the shell that I wanted to edit File_B. Well that seems pretty easy, right? Because I am already located in a directory that contains the file (/home/joe), when I tell the shell "File_B", it finds the file immediately in my current directory.

Now, one last example that combines both of these concepts. Let's assume that there is a directory within my home directory, called More_Files. Inside that directory is a file called File_D. How would I specify that file's absolute path? It's relative path?

For the absolute path, I would specify it like this: /home/joe/More_Files/File_D. Pretty straightforward. As you can see, the root directory is first (/), then the next directory within that one (home), then the next directory within that one (joe), then the directory containing the file (More_Files), and finally the file itself (File_D). You're basically telling the shell how to find that file no matter where you are in the system, because no matter where you are, that file will always have the same absolute path. In Windows, this is like saying you want to see the file C:\WINDOWS\system32\windoze.exe. All directories are specified.

For the relative path, I would specify it like this: More_Files/File_D. Notice that there is no slash (/) at the beginning. That is because we don't want to specify the root directory first, we instead want to tell the shell to look in our current directory for the directory "More_Files", and within that directory, "File_D." Had you specified the slash (/) at the beginning, the shell would have assumed that you were giving it an absolute path (because the root directory contains every other directory on the system), and thus it would have looked in the root directory (/) for the directory "More_Files", would have not found it (because it is actually in your home directory), and you would have never been able to get to the file "File_D." In Windows, this is like already being inside the C:\WINDOWS folder, and trying to get to system32/windoze.exe. Had you specified C:\system32\windoze.exe, Windows would never find your file (remember that in Windows, the drive letter is the "root" directory of that filesystem), because there is no "system32" directory in the "C:\" drive.

Okay, now for some commands!


Basic Commands

The basic commands that I will cover in this tutorial (with examples) are man, ls, cd, pwd, cat, and more.

The "man" Command

The man command is probably one of the most useful commands to ever exist in the Linux/Unix world. Basically, you simply type the command "man", and then an argument to the command. An argument is simply an extra parameter to a command. The man command requires a single argument.

Once you send this command (by hitting "Enter"), you will see a page appear, that has all sorts of detailed information about the command that you provided as an argument. In other words, you can type "man <command>" (replace "<command>" with the command that you want to know more about), and you will get a page full of detailed information about that command (how to use it, available options or arguments, etc.). Below is an example:

The

Then I hit "Enter", and I got a "man" page!

The

As you can see, the standard "man" page (or, "manual page", for which the command is named after) will tell you the name of the command (NAME), how to use it (SYNOPSIS), and some good information about the usage of the command (DESCRIPTION). If the manual for a command is longer than your terminal display, you can simply scroll up and down using the arrow keys. Alternatively, you can press the Spacebar to go forward a whole screen, and the "b" key to go back a screen.

If you are ever confused about a command, remember that you can always "man" that command, and get some more information about it.

The "ls" Command

The ls command allows you to list the contents of a directory. You use it simply by typing ls and hitting "Enter" within any directory. Below is an example:

The

In this example, I am located within the "example" directory in my home directory (as is indicated by the Bash prompt). I typed "ls", and was shown the contents of the directory I was in (the "example" directory). As you can see here, there are six items in this directory: dir1, dir2, file1, file2, file3, and file4. Notice also that dir1 and dir2 are both colored blue. This is a feature that the Bash shell supports, as long as your system is displaying colors on the screen, and the terminal program supports it as well. It likely does, and in this case, the two directories that were listed are displayed blue, to let me know that they are directories (the colors are also customizable).

This command also has many options that you can specify along with it, and each option performs different actions. Options are usually specified with a dash (-) before the option, in order to let the shell know that you are indeed specifying an option, and not something else (such as an argument).

For example, to perform a long listing of a directory that will show you specific information about each file in a directory, specify the "-l" option along with the command:

The

As you can see in the image above, after adding the "-l" option to the command, I received more information about each file in the directory. In order, I have received the file permissions (or directory permissions), the number of links, the owner, the group, the file size, the date and time it was last modified, and the file name. Don't worry about what they all mean, we'll cover this in a later tutorial.

In addition to being able to list the contents of the current directory, you can also list the contents of another directory. To do this, simply add the name of the directory after the command. By doing this, you are specifying the target directory as an argument to the command. An argument is basically an extra parameter to a command. Some commands will require an argument, while some don't. Some may require more than one argument. The man command, for example, requires a single argument.

An argument for

As you can see, when I specified "dir1" as an argument to the ls command, I received a listing of the contents within the dir1 directory. I, however, did not leave my home directory. I was simply shown the contents of another directory.

Alternatively, I can also perform a long listing of another directory:

An argument with the

Here, I received a long listing of the contents of the dir1 directory, by giving the ls command the "-l" option, as well as passing it an argument (the "dir1" directory).

There are many, many options to the ls command, but here I've covered its basic usage. If you would like more information, you can always "man" this command (by typing "man ls").

The "cd" Command

This command is quite simple, really. It stands for "change directory", and it does just that. You use it to tell the shell that you want to move to another directory. Remember, you can specify a directory using either absolute path or relative path. You use it by providing a single argument to the cd command, which is the location of the directory you would like to go to. Here is an example:

The

Pay attention to the Bash prompt. At first, I have listed the contents of the directory that I was in (which was ~/example). Then, I typed "cd", along with the directory I wanted to move to, "dir1". Then, my Bash prompt changed to say ~/example/dir1, to reflect that I was now located in that directory. Note that when I listed the contents of my current directory a second time, I got a different listing, which implied that I was now in a different directory than I was before I used the cd command.

The "pwd" Command

The pwd command stands for "present working directory." It will display the directory where you are currently located. It takes no arguments. This is useful when you're not sure where you are in the system. Of course, the default Bash prompt will display this information, but as the prompt is customizable, the pwd command is still important to know. The pwd command also updates the PWD environment variable, so this variable always contains the absolute path of your current directory. Simply type "pwd", with no arguments, to see the absolute path of your current directory:

The

Note that my Bash prompt was displaying ~/example/dir1, while the output of the pwd command displayed /home/joe/example/dir1. Remember that the tilde (~) represents your home directory in the Bash shell, so both ~/example/dir1 and /home/joe/example/dir1 will be interpreted in the same way by the Bash shell, and represent the same location in the system. One of them is just obviously easier to type than the other.

The "cat" Command

The cat command, in its basic form, is quite straightforward. It stands for "concatenate". You can either specify a single file, which cat will read in its entirety, or you can specify more than one file, which cat will combine (one file after the other) and subsequently display. Let's see some examples:

The

Here, I specified a single file as an argument, file5. So, the cat command displayed the contents of file5, which has a single line that says, "This is file5." Now let's specify more than one argument:

The

Did you notice that both file5 and file7 have a single line within them, each specifying the name of the file that you are viewing? But once I performed a "cat" on both files at the same time, I got the combined output of both files! Now, I didn't overwrite anything, and no new files were created. I simply got the combined output of both files, in the order that I specified them. If you want to create a file from this output (or any output for that matter), you would need to redirect your output to a file. I'll cover that in a later tutorial.

Now let's see what happens when we try to "cat" a file that contains many lines:

Multiple lines

Everything looks okay, right? Well, it does, until you scroll up (presumably using your mouse):

Scroll

Ahh, there's a line that we missed. We didn't see it because there were more lines in the file than the terminal window could display. Of course, we can always resize the terminal to fit that one extra line, and there would be no problem. But what do you do when the file has hundreds, even thousands of lines? Surely you can't resize the terminal to be that big. Enter the more command.

The "more" Command

The more command does just what its name implies: it shows you more. This command is useful when you have a large file that you want to read, or you simply don't want to read the entire file (maybe just some of the first lines). This command will stop displaying any output that takes up more space than your terminal can display. It will pause, until it gets some input from you to continue. If you played at all with the "man pages" from the man command (if you read these commands in order, then you may very well have done this already), you can scroll up and down in much the same way. You use the up and down arrow keys to scroll up and down by one line, and you can press the Spacebar to scroll down an entire screen's worth of lines. Let's see an example:

The

Here I am passing "file6" as an argument to the more command (this was the file in the cat section that had more lines than could be displayed at once). Once I hit "Enter", I get the output shown below:

First screen of

Note that Bash is also telling me how far into the file I currently am. Here it says I am 69% through the file. Once I am finished reading the first screen of output, I can hit the Spacebar for the next screen:

Second screen of output

And it just so happens that the next screen of output was the end of the file. Once the more command detects that you've hit the end of the file, it will end and place you back at the Bash prompt.

Well, that's it for this tutorial, I hope you got a good understanding of how the Bash shell works, and how to use some basic commands. If you feel you are ready, then look for the "Bash Shell Basics - Level 2" tutorial, coming soon!

Last Updated on Friday, 06 February 2009 16:48
 

Add your comment

Your name:
Your email:
Comment: