In the beginning was the command line and yet these days it's considered the realm of the neckbeard hackers. The mass media use command lines to illustrate digital-misbehaviour and Hollywood uses it to prove that the character knows something super-duper-clever about computers. However, in reality, the command line is a critically powerful tool which no self-respecting reader of this blog would ever go without knowing, understanding, and ultimately loving.

Almost every Linux desktop you find (and Mac OS X, and in fact Windows) has a terminal application of some kind. It might be called Terminal or Console or perhaps something more technical-looking such as gnome-terminal or urxvt. Regardless of its name, the purpose is the same. To provide you with a window onto the underlying power of the command line.

The command line is often referred to as an interactive shell and in later articles we will speak more about the shell as a whole. We talk about shell prompts when we are talking about interactive shells. The prompt is how the shell program tells you that it is waiting for you to tell it what to do.

Almost all shell programs have sh in their name, typically at the end. You will find a plethora of them available, from the humble dash through the very common and well used bash to the super-powerful (and somewhat hard to understand at times) zsh. There are two major families of shells in the Linux world. Those which are considered Bourne shells and those which are considered C shells. In the former are bash zsh ksh and similar. In the latter are tcsh and friends. These two families represent very divergent syntaxes (the language they implement) and we will mostly concern ourselves with the Bourne shells in Yakking articles.

My first shell prompt

When you open your terminal program, you'll likely be faced with a squareish window with a single bit of monospace text in the top-left of the window. Depending on your terminal it'll typically be white-on-black or black-on-white, however many other colour schemes exist, and you can usually customise it yourself if you wish.

myuser@mycomputer:~$ 

You'll usually see a flashing box or underline next to that text. This is the prompt and we'll break it down in a moment. It is worth noting that this is just the default bash prompt for Debian and derived systems. If your shell is dash (or even the venerable sh itself) then your prompt may be as simple as a single $ sign. If your shell is an unconfigured zsh then it may be simply mycomputer$.

The bash prompt above has four major parts. The first two before the colon and the second two after it. The first part is the user name. This is the name you use when you log into your computer. Then you have an at-sign followed by the short host name of the computer. After the colon you have the path you are currently in. This is the full name of the directory (or folder) where your shell is currently residing. We will see more about directories later. Finally the $ indicates that your shell is not running with administrator privileges (as root). If instead of a $ you have a # then you know that the shell is running with elevated privileges and that you should be super-careful.

Navigating the filesystem

Up until now, you've probably been used to navigating your filesystem in a file manager such as Nautilus, Thunar or Konqueror. You may even only have interacted with the filesystem through the load and save dialog boxes in your GUI applications.

Every program on your computer has a concept called current working directory (or CWD) which is the area of the filesystem where the program is currently running. Your shell is no exception to this. In the example above, the CWD was ~ which is actually a shell shortcut for user's home directory. Often this will actually be something like /home/username but it may not be.

To change directory in the shell, we can use the cd command which is built into the shell:

myuser@mycomputer:~$ cd Desktop
myuser@mycomputer:~/Desktop$ 

As you can see, when you enter a command (and press ENTER) the command is run and then the shell prompts you again. Only this time, the shell's CWD has changed and so the prompt alters to reflect that. In every directory there are two magical directory names which represent here and parent (. and .. respectively):

myuser@mycomputer:~/Desktop$ cd .
myuser@mycomputer:~/Desktop$ cd ..
myuser@mycomputer:~$ 

You can also specify the full path to a directory to change into, for example:

myuser@mycomputer:~/Desktop$ cd /home/someoneelse
myuser@mycomputer:/home/someoneelse$

If you are not sure what the exact name you need is, you can ask for a listing of files, both in the current directory or in any specific directory of your choosing:

myuser@mycomputer:~$ ls
bin      Desktop   Documents  Welcome.txt
myuser@mycomputer:~$ ls /
bin   etc         lib         media  proc  sbin     sys  var
boot  home        lib64       mnt    root  selinux  tmp  vmlinuz
dev   initrd.img  lost+found  opt    run   srv      usr
myuser@mycomputer:~$ 

Not everything that the ls command shows you is a directory, but typically in modern GNU/Linux (or similar) systems, it will colour its output to show you what is what. If it is not colouring things in for any reason, you can ask it to give you a marker for each entry to indicate what it is:

myuser@mycomputer:~$ ls -F
bin/      Desktop/   Documents/  Welcome.txt
myuser@mycomputer:~$ 

In the above example, directories are shown with trailing slash markers (/).

The cd built-in command also has a couple of very neat tricks up its sleeve. A bare cd will change back to the user's home directory:

myuser@mycomputer:/somewhere/odd/goodness/knows/why$ cd
myuser@mycomputer:~$

If you say cd - then you switch back to where you were before:

myuser@mycomputer:/somewhere/odd/goodness/knows/why$ cd /bin
myuser@mycomputer:/bin$ cd -
myuser@mycomputer:/somewhere/odd/goodness/knows/why$ cd -
myuser@mycomputer:/bin$

Running programs

In the previous section, we saw two kinds of action at a shell prompt. Running a built-in command such as cd or running another program such as ls.

Commands you typically run at the command line will often have very short confusing looking names (until you get used to them) such as ls, pwd, grep, xargs etc. Do not worry too much about this for now. We will cover essential shell utilities in another article in the future.

When you run a command at the command line, you can give it extra information on what to do; these are called arguments. Above we gave ls the extra argument -F which told it to annotate each entry so that we could tell what kind of entry in the filesystem it actually was. Commands may take between zero and many-lots arguments and may change their behaviours based on those arguments. Where arguments begin with a dash or double-dash, we call the argument an option or switch. (In MS-DOS, switches started with a slash instead).

Editing your commands

The shell's interactive prompt is also often called the line editor as you can actually edit your command line before running it.

If you are typing in a long command, you may find that you need to go back and edit something early on in the command line. There are a number of keys and key combinations which can help you to edit your command line. The simplest and perhaps most obvious of which are the Left and Right arrow keys which move your cursor left and right along the command line you are entering right now. Other handy keypresses (bash specific in some cases) include:

  • Control+A: Go to start of line
  • Control+E: Go to end of line
  • Control+K: Delete from cursor to end of line
  • Control+U: Delete from cursor to start of line
  • Control+Left: Go left one word
  • Control+Right: Go right one word
  • Control+D: Delete character to the right of the cursor. (Note, sometimes your cursor is represented by a block not a line. In that case, the character underneath the cursor is the one which will be deleted). There's plenty more but those are the basics.

Delving into history

The shell keeps a log of the commands you run. It will even save this when you leave an interactive shell and restore it when you start your next interactive shell. The history can be accessed in a variety of ways. The most commonly useful are the Up and Down arrow keys. These go back and forward through the history of commands you have entered.

The next history command you should learn is Control+R which provides an unusual prompt:

(reverse i-search)`':

As you then type, the shell will search backwards in the history looking for a command which matches what you type. When you find the command you want, you can use the Left and Right arrow keys to then bring it into the line editor for you to work on before pressing Enter to run the command.

There are many more history related keypresses and shortcuts, but they are a topic for a future article here on Yakking.

How do I get out?

There is a shell built-in command called exit which will leave the current shell. If you enter that at a prompt, the shell will exit and the terminal window it was running in will likely close automatically.

Conclusion

The interactive shell is a simple but immensely powerful way to have a conversation with your computer. It is not something to be scared of, but rather something to learn to master to improve your general productivity. You can often do quickly on the commandline something which might take a significant amount of clicking around to achieve in a GUI.

If you want to find more, you can look at the shell related articles here on Yakking, read the article linked in the first paragraph of this article or just get going and see what you can find out just by playing with your shell prompt.