CHAPTER THREE: Linux Shells
What is the Linux Shell?
In Linux, a "shell" is a command interpreter. Put simply, the shell is a program that helps the user to communicate with the rest of the operating system. Shell programs make it much easier to communicate with the machine and tell it what to do.
Brief History
Once upon a time, computers were massive machines that took up a large part of a room. They didn't have screens, keyboards, or mice; instead, users communicated with them through manually feeding them instructions on paper cards by using teletype machines or a series of switches. Even though these machines were massive, they could only run one program and the program instructions had to be re-fed to the machines every time they were turned on or a new program was needed.
Someone realized that computers could do multiple tasks if they dedicated 30% of the time to Paul, 30% of the time to Betty, and 40% of the time to Pat. The computer could run Paul's program for 30 seconds then Betty's program for 30 seconds, then Pat's program for 40 seconds. One of the first multi-tasking operating systems was named Unix. As discussed above, the method of communicating with the operating system using a shell which evolved from the old paper card programs.
Over time, computers started getting smaller but the concept of how to communicate with them, along with a lot of the terminology carried over from the early days stuck, including the idea of shells and terminals. Originally, the terminal was a teletype that looked like a typewriter and display. The terminal provided a more convenient way to feed information to a computer while the shell provided a way to process the keyboard inputs.
Eventually, developers who wanted more user-friendly machines and to get computers in the hands of more than just programmers created less complicated operating systems where the work of the shell and its terminal could be hidden from the user. Thus, operating systems with desktop environments were developed.
Back to Shells
The shell of an operating system is a program that takes input from the keyboard and gives it to the operating system as commands to perform. The shell communicates with the terminal that the user uses to interact with the operating system.
In a desktop environment, the shell communicates works without being seen. If it's a graphical user interface (or GUI ) all the user has to do is navigate to certain icons or click on certain buttons to get the machine to perform tasks or open programs. GUIs aren't the only means of communicating with the shell, however.
The shell also communicates with the line user interface , or LUI , which can also be referred to as the command line interface , or CLI . An example on Windows is the Windows Command Prompt, which is the black screen that prompts the user to enter code so the computer can perform certain tasks.
Although you can run programs using a GUI in Linux, using the LUI to execute programs is far more common because the LUI is very powerful in the Linux system. The shell is typically used to start other programs.
The Terminal
In Linux, the terminal is actually a terminal emulator. The user is usually unable to communicate with the operating system without the use of the terminal, which is partially due to security purposes.
Note that the shell itself is a program that receives input from the terminal or GUI desktop - it is not the same thing as the terminal. Multiple terminals can be used to communicate with the same shell. However, although a terminal can talk to different variations of shells, it can only address 1 shell at a time.
The terminal is the screen that used to give the shell various commands. In Linux, sometimes only a terminal CLI is available because the machine does not need to run a desktop environment. This is okay for most computers and may even be necessary for those that have limited memory or do not need to have a screen the user can interact with.
Why Use a Command Line Interface?
For some smaller computers, such as what you may commonly find in robots or remote-controlled machines, user interaction with a desktop environment is impractical. In these cases, the machine is accessed by typing commands into the command line.
Even though it can seem inconvenient to those familiar with using a desktop, using the command line is faster, more powerful, and more practical. For space reasons, servers don't include a keyboard, mouse, and display so they must be accessed using the CLI. Small devices such as a home router may run a Linux OS and must be accessed with the CLI.
Now that you know a little about shells, terminals, and the command line, let's talk about the types of Linux shells and why you might want to use each of them.
Types of Linux Shells
In this chapter, we'll be talking about the two kinds of shells you may find in Linux: Bash and csh. Bash was originally used on Unix. The Apple OS was based on Unix and uses Bash so if you are familiar with the Apple OS command line, a lot of this may be familiar to you.
Mac and Windows Users
However, even if you are familiar with the Unix command line, there may be slight differences in Linux syntax.
If you are a Windows user, unfortunately, a lot of this may seem foreign to you. Even though Windows has the Windows Command Prompt, you may not be familiar with it if you have only been using your desktop environment to perform tasks.
Where To Run A Linux Command Line
There are different ways to get a Linux command line for learning.
Install Ubuntu
The best way is to install Ubuntu. There are a number of options for doing this including installing a dual boot Ubuntu image, running from a USB flashdrive (called USB Live), or installing Ubuntu on a VirtualBox image.
Dual Boot (Native Install)
Follow the Ubuntu tutorial to download the image to a USB flashdrive and install it.
- Pros – Choose whether to run Ubuntu or Windows at startup.
- Cons –Must reboot to switch to the other OS.
USB Live
Follow the Ubuntu tutorial to download the image to a USB flashdrive and but only run it from the USB drive.
- Pros – Does not change the hard drive in default.
- Cons – Harder to save anything. Changes don’t persist – a reboot will go back to the initial state.
VirtualBox
Create a new VirtualBox environment and Ubuntu tutorial to download the image to your PC and install it.
- Pros – Can switch between different OSes easily.
- Cons – Uses space on hard drive. Slower than native install.
Webinal – Command Line in Browser
Another option is running the Command Line in a browser. Web options such as Webinal allow you to learn the Linux command line using a browser. In fact, a lot of the examples in this book use webinal.
Chrome OS and Others
If you already have a machine that runs an OS based off Linux, such as Google Chrome, you may fall into one of two camps - 1) you may have never even heard of the Crosh terminal and may have stuck to using your machine for light tasks, such as doing schoolwork, surfing the internet, or streaming movies and TV shows. In this case, you might have little to no knowledge of the Linux command line and little to no experience using a terminal.
On the other hand, if you've completed more advanced tasks with your Chromebook, you might have some familiarity with its inner workings and the Crosh terminal. In that case, some of this book may have been familiar to you, namely commands like sudo, apt-get install, among others.
That said, even though Google ChromeOS is based on the Linux operating system, they are not the same. If you are using dual OS mode to follow along with these exercises, make sure that you are not in Google Chrome and that you are not typing in a Crosh terminal. Switch to the Linux side to complete these exercises there to avoid any confusion.
Useful Commands
Tab-Completion
If you want to re-type a command but aren't sure what it looks like, you can start typing the command and press the Tab key. The terminal will attempt to complete the command for you, preventing you from having to stop to look up syntax and spelling for commonly used commands.
CTRL+R--Search History
To use this command, tap the Ctrl+r keyboard shortcut. You should get an option to search the history of commands you have typed into the terminal. Type in a few letters of what you're looking for and the last command you entered that contains those letters should show up.
You can also use the Up Arrow key to view the last commands you used to a point.
Other Shells
csh
The abbreviation csh stands for C Shell in Linux, or you may also get the version called Tcsh . The C shell is an LUI interpreter with a C programming language-like syntax. If you are not already familiar with the C programming language, you may want to familiarize yourself with it before you attempt to do any serious programming in a C shell.
Brief Overview of Shell Scripts
You can use either Bash or csh shells to run programs from a terminal. However, this book will cover Bash shell scripting.
The difference is the latter of the two will accept inputs using C-like scripting syntax. However, teaching C language is beyond the scope of this book, and may make learning Linux seem complicated. For now, we will stick to the Bash shell for Linux commands.
Chapter Summary
- csh and Bash are both types of shells used in Linux.
- csh, or C shell, is a type of shell that uses C-like syntax and scripting language.
- A terminal and a shell are not the same thing - a terminal is a program used to allow the user to give instructions to the shell.