Linux Lesson 02 - Introduction to the Shell
In this lesson you will learn about different Linux interfaces and how to execute commands.
Linux, and Operating System with Many Faces
There are many ways to run Linux. You will find it is extremely customizable to fit whatever preference you may have. There is a graphical component of Linux which gives you a desktop environment in which you interact with a keyboard and mouse. This is called a GUI (Graphical User Interface). There is also a command line component of Linux which takes all input from a keyboard.
You can choose from many available desktop environments. Some will look and feel like Windows, and some will look and feel like Macintosh, and still others will be completely different. Some desktop environments are designed for high end computers, while others are designed to run on slower, aging hardware. You have the ability to run whatever desktop environment you prefer. Some of the popular desktop environments are
GNome - Very popular desktop environment. Version 2.x was the default on many distributions, version 3.x upset many people and cause some distributions to change to something else, including Ubuntu. GNome uses the GTK (GIMP ToolKit) toolkit for its applications.
KDE - A very Windows like desktop environment which is very customizable. Uses the QT toolkit.
XFCE - This is a lightweight desktop environment based on GNome version 2.x. This will work well on older hardware.
Unity - Desktop environment used with Ubuntu. This uses the GTK toolkit.
Cinnamon - A desktop environment based on GNome 3.x, but visually closer to version 2.x. It is a blending of the two worlds, newer libraries, and the look and feel of 2.x.
MATE - A desktop environment based on GNome 2.x. It's picking up where GNome 2.x left of and adding improvements to it while still maintaining the look and feel of Gnome 2.x.
LXDE - An even lighter weight version of XFDE, this is designed for older computers and netbooks.
The same is true for the command line interfaces. The command line interpreters are called shells. A Shell interprets the commands you type and executes it. Just like with the desktop environments there are many shells available to you.
Bash (bash) - The is the default Linux shell in many distributions of Linux. It is a combination of both the Bourne shell and the C shell. It provides the flexibility of the C shell in a Bourne shell environment. Bash = Bourne Again SHell.
Bourne (sh) - This was the original Unix shell developed by Steve Bourne at Bell Labs.
C Shell (csh) - A shell created to have a similar syntax to the C programming language.
Korn (ksh) - A combination of C shell and Bourne shell that was developed at Bell Labs.
The Bourne Again Shell (Bash)
In our class we will be using Bash as our shell. You run the bash shell as either a regular user, or as root. Running as root is the equivalent as running as administrator in the Windows world. You can tell if you're logged in as root, or a standard user by looking at the character at the end of the Bash prompt. If it's a $ you're logged in as a standard user and if it's a # you're logged in as root. It's not recommended to run as root all the time.
When you first open a shell you'll see it waiting for you to type something. It will sit there until you tell it what to do. You tell it what to do by typing in commands. Commands are case sensitive, the command cal will work, but the command CAL will not. After typing a command hit enter to execute, or run, the command. If the command has any kind of output it will display it on the screen below the command you typed. Once the command is complete it will display another prompt waiting for the next command. You can modify what the command does by adding options. An option tells the command to run a different way. In the image below we use the cal command to show a month calendar. In the next command we add the -3 option and that tells the cal command to show three months at once. Options are preceded by a dash and can be combined.
You can also pass arguments to your commands. These are usually something the command will act upon, or use when performing the task. If you were making a new directory in Linux you would use the command to create the new directory, and pass to it the name of the directory you want to create.
The structure for the commands in Linux is consistent. They follow the format command -option argument. The options are optional, and not all commands need arguments. When you're new to Linux building the commands can be confusing, but if you remember the format it will help you.
Some commands don't have any output. For example if you use the command that copies data from one spot to another it will do the task then return to the prompt. In a situation like this no news is good news, if it doesn't output anything then it worked. It it does output something then their was a problem.
More Advanced Command Structures
Some commands will send their results to the screen. In the image below you can see use the cat command to display the contents of the hosts file to the screen. While the host file isn't that big, you may find yourself working with larger text files in the future. You can use the pipe character | to send the results of one command to another command. In the image below we can see we are sending the results of the cat command to the grep command. The grep command is used to search for a string. We're telling the grep command to search the input for the string localhost, and output the entire line if found. The result is the lines we want from the text file.
You can redirect a command's output to a file instead of the screen. Using the greater than character > you can send the output to the file listed after. If the file doesn't exist it will create it. If anything exists in the file it will overwrite it. You can choose to append, or add to the end of the file by using two greater than characters >>.
You can combine many commands into one line. The semicolon ; or two ampersands && will combine two commands together. Once the first completes the second will start. In the image below we see two commands joined together. The first will get a list of all updates for the system, and the second will install the updates. This will update the computer with one string of commands.
As you use the shell the system automatically remembers every command you use and stores it in the .bash_history file in the root of your profile. You can flip through your command history by pressing the up arrow on the keyboard. You can also have the system auto complete command names and file locations by taping the tab key.
Some commands won't execute as a standard user. In order to run those commands you have to use the sudo command to execute the command as root. If you have the right permissions you will be able to run the command.
If you want to learn more about a command, or see what options are available you can read the command's manual. The man command will display the manual for any command. You open the manual by typing man command. When the manual opens you can use the up and down arrows to scroll through it. If you want to search the manual type a forward slash / followed by the search term. After a search you can hit n to jump to the next result. (Shift + n will jump in reverse.) When you're done with the manual hit q to return to the shell.
If you want a quick look at the syntax of a command you can type the command followed by --help and it will display help information. Here are the results for of cp --help
As you can see not all the information fits on the display. If this happens you can send the output of the cp --help command to the more command and it will show one page at a time. cp --help | more
The more command is very limited. It will display one page at a time and close when it gets to the end. Another command, less, will do the same thing as more, but give you more control when you view the text of the screen. You will be able to scroll up and down in the text and perform searches using the same method as the man command. It is said that less is more. So if you ever type a command and the result scrolls too fast for you to see it try piping it to less so you can see it all. cp --help | less
If you are unable to find what you need in the manuals or the help text then the next place to get help is the Internet. There is a lot of information on Linux online and a web search will get you started.
When Unix was created it ran on hardware that was much larger than we're used to today. Entire rooms, or floors would be dedicated to the equipment. The idea of each person having their own computer wasn't an option. Punch cards containing the instructions you wanted to execute were feed into the machine and the results were returned. You would have to wait to get access to the computer, then wait for the results. If there was any error in your commands you would have to start the process over. The computer would work on one stack of cards until it was done, then move on to the next one. This was called batch processing, the computer would only do one thing at a time.
As time went on they added the ability to type these commands using a keyboard and send them to the computer instantly. They repurposed teletype machines to interact with the computer. Multiple teletype machines could access the computer at the same time. With this change computers had to work on multiple things at once. A computer can only work on one thing at a time, but they added the ability to switch between tasks quickly giving us the illusion of multitasking.
In order to facilitate the teletypes connecting to the computer they had to create interfaces for them. Each session was numbered with a tty prefex for the teletype interface. These interfaces still exist in our environments today. You can use Ctrl + Alt + F1 to switch to tty1, and Ctrl + Alt + F2 to switch to tty2. This will work up to tty6 on our Linux computers. The graphical console is tty8 on the distribution we use in class. When you are done using a tty you can logoff by typing exit or logout or you can hit Ctrl + D (D for die)