Saturday, October 3, 2009

Linux Shells - Specifically the BASH Shell

Shells

In *nix(Unix/Linux) the shell is a text-based interface to the operating system. The kernel is the lowest-level innermost part of the operating system, so the shell is so named because it surrounds the kernel. Over the years, various shell programs have been written. The first was the Thompson shell (commonly referred to as simply sh) written by Ken Thompson at Bell Labs. Other popular shells have that have been written over the years include the Berkeley Unix C shell (csh), the TC shell (tcsh; the T comes from the TENEX operating system), the Korn shell (ksh), the Z shell (zsh), the Bourne shell (bsh), and the Bourne-again shell (Bash). Bash was the shell originally developed by GNU for the GNU operating system. It is commonly available in most GNU/Linux distributions and this manual is written assuming you are using Bash.
You can see which shells are installed on your system by typing cat /etc/shells (the chsh -l command simply displays this file). Most likely if you are using Linux, your default shell is bash. An easy way to tell what shell you are running is to type less /etc/passwd and look for the line containing your username. The last entry on the line will be your default login shell. If you wish to change your default login shell you can use the chsh -s shell command. This will modify the shell entry for your username in /etc/passwd so the next time you login you will be running the new shell.

The Bash Shell

Once logged in to linux, you will see a window with some text, awiting your input. This is called a terminal window or just terminal; you may also hear the term console window or just console.

The $ prompt is the Bash shell prompt. It is Bash's way of letting you know that it is waiting for you to type a command. The ~ symbol in Bash always refers to a user's home directory. Your home directory is where you are placed when you first log in and it is the area in the file system where you can store your files. The prompt string can be changed(discussed in later article).

When you type commands at the prompt and press the Enter key, Bash will read the command string you typed in, parse it to break it into words and operators, perform some post-processing, and will then execute the the command. Unless you tell it otherwise, Bash will wait for the command to finish executing before displaying the $ prompt again. Some of the commands that you may use are built-in to Bash, i.e., the code that gets executed is inside the Bash program itself. However, most *nix commands are standalone programs that are found in the directories /bin, /usr/bin, and /sbin directories. For example, we can determine where the ls command is located

$ whereis ls
ls: /bin/ls
whereis is a command which will search for the program file given to it. In this case we are searching for the program file named gcc. whereis tells us that a file named "ls" can be found in a directories. In fact, the first one /bin/ls is the actual executable file which is the ls command. For fun, try to determine where the whereis program is installed,

$ whereis whereis
whereis: /usr/bin/whereis /usr/share/man/man1/whereis.1.gz


We can see that it is installed in /usr/bin. In fact, most of the basic *nix commands (i.e. executable programs) are located in this directory. Bash will also execute special programs called scripts written in the Bash shell scripting language. See Chapter 11 for a basic introduction to Bash shell scripting. Overall, Bash is a very complex program, and we will only learn a small part of it in this course. However, what we learn in this course is supposed to provide you a solid foundation on which you can learn and obtain more knowledge of *nix and Bash.

No comments:

Post a Comment