Introduction
It is our firm belief that Linux, despite its advances on the desktop side, as well as on tablets, should be taught starting with the command line. That’s because it’s an operating system that borrows heavily from Unix(TM), and there was nothing more than a text interface on it at the very beginning. Studies have shown that, if applicable, one is more efficient on the command-line rather than using a graphical user interface (GUI). “If applicable” means that we’re not referring at photo/video editing or some other task that requires a graphical environment. It means that when there is a task that can be solved either via the command line interface (CLI) or via GUI, the CLI way is more efficient. Another thing to be considered is the fragmentation of the Linux world. For example, OpenSUSE’s YasT won’t be available on any other distro, so it’s a specific piece of software. This fragmentation is seen also in the CLI world, especially when it comes to the locations of various files, but we will make you aware of it, should that be the case. In case you’re not convinced yet, remember that you don’t know Linux, or any other similar operating system, until you know your way around its CLI. The power of Linux lies there, and if you want to make a career out of it, follow along: it’s an interesting and fun journey.
Internal vs external Linux shell commands
You may be puzzled by this choice of words, and with good reason. But it’s a terminology you will encounter often, along with the term “(shell) built-in” when referring to internal commands and perhaps “the rest” for the external ones. But before we go that far, let’s make sure we’re on the same page. The shell we’re gonna work with is bash, as it’s the most used on Linux distributions. That’s not saying it’s the best, but that’s a subjective term anyway. I don’t know of any popular and still maintained Linux distribution that uses any version of bash prior to 4.xx, so that’s what we will use too. Regarding the ever-controversial distribution support, LPI seems to focus mainly on Red Hat and Debian or derivative distributions (e.g. Fedora or Ubuntu), so this is what we’ll support as well. However, at this level at least, the distribution is less relevant: what is important is an up-to-date shell and distribution.
Please make sure that you have a shell ready, either in a pure console environment or in a GUI, as our web pages are written with text-only friendliness in mind. Make sure your shell is bash, and let’s start! You can check the shell by typing
$ echo $SHELL
Unless your distribution hides its shell under peculiar locations, the above command should return /bin/bash
. Regarding the version, here’s how you can check on Red Hat or Debian plus derivatives:
$ rpm -q bash #on RH $ dpkg -l bash #on Debian
Now that we’re certain we’re running the right version, let’s see what internal and external commands are. The internals (a small number, especially compared to the rest of them) are the commands that are built in your shell. This is why we deemed important to check the shell and the version, because other shells may have different built-ins or none at all. Commands that you will use very often like cd – change directory – are shell built-ins. Ironically, there is a shell built-in that tells us if a command is a built-in or not. Yes, I know, sounds a bit crazy, but that’s the truth. The command is type and it’s usually used with no options, just the name you want to know about. Observe:
$ type cd cd is a shell builtin $ type bash bash is /usr/bin/bash $ type type type is a shell builtin
Now, if you want to see all the built-ins bash offers, you can either check with your shell’s documentation for in-depth explanations that are a bit outside the scope of this document, or you can read on and get a short description of some of them, as follows:
alias |
This command allows you to define commands of
your own, or replace existing ones. For example, ‘alias rm=rm -i’ will make rm interactive so you don’t delete any files by mistake. |
break |
Used mostly in shell scripting to break the
execution of a loop |
cd |
Change directory. For example, ‘cd /usr’ will
make the current directory be /usr. See also pwd. |
continue |
Used mostly in shell scripting to continue the execution of a loop |
echo |
List the value of variables, either
environment-specific or user-declared ones, but can also display a simple string. |
export |
Allows the user to export certain environment
variables, so that their values are used to all subsequent commands |
fg |
Resume the execution of a suspended job in
the foreground. See also bg. |
history |
With no arguments, gives a numbered list of
previously issued commands. With arguments, jumps to a certain number in said list. |
kill |
Send a termination signal by default, or
whatever signal is given as an option, to a process ID. |
pwd |
Print working directory |
read |
Used mostly in scripts, it is used to get
input from the user or another program |
test |
Used with an expression as an argument, it
returns 0 or 1, depending on the evaluation of said expression |
times |
Print the accumulated user and system times
for the shell and for processes run from the shell. The return status is 0. |
type |
Indicates what kind of command is the
argument taken. |
unalias |
See alias |
wait |
Usually given a process id, it waits until
said process terminates and returns its status. |
If you think you won’t be able to learn all the internal commands, don’t worry. Further articles will deal with bash and inevitably we will have to deal with more built-in commands. Anyway, it’s strongly recommended you read the bash manual page, and try to practice as much as you can, especially since some of the exercises at the end of this will deal with some simple internal commands that were intentionally excluded.
Let’s see what external commands are. They are the commands that your system offer, the ones that are totally shell-independent and usually can be found in any Linux distribution. They mostly reside in /bin and /usr/bin, and those locations must be part of your $PATH variable in order to be usable. Commands used mainly by the superusers/sysadmins are to be found in /sbin and /usr/sbin and usually require root privileges to run. So /sbin and /usr/sbin are in root’s $PATH but not in a “normal” user’s. In short, this is a environment variable that holds, in order, the location of the external commands available to a user. That means, if I have /bin in my $PATH, I can type ls and it works, instead of typing the full address of the external command, namely /bin/ls.
Exercises
1. Find out what your $PATH is, and compare it to the root user’s $PATH. Why do you think that is? Explain.
2. Find out what the . (yes, that is a dot) internal command does and why is it useful.
3. There are a few built-ins that have the same name as external commands. How would you find them?
4. What return status can echo have? Explain.