Black Magic – Demystifying the Command Line Interface

cli

In a previous post, I wrote about how I migrated to Linux after many years running Windows. For most computer users with longstanding Windows experience, one of the biggest gripes against Linux is that it is not user friendly. For many such people, the mentality is based on hearsay, but for some, it is based on actual experience: What do you mean sudo?

Fear

Few things strike fear into the hearts of modern Windows users like the CLI, and it’s easy to see why. Microsoft has come a long way with its flagship product, the Windows OS. Each new version has endeavored to give the user a more pleasant experience than the last. It’s primarily the reason for the product’s massive popularity, in spite of its price, security vulnerabilities and other flaws. The Windows desktop user has become accustomed to the easy life. Pointing and clicking is the system’s lingua franca. Application software developers have not “helped” the situation. Nearly every useful program, however cryptic, has a Windows GUI provided either by its vendor or by a third party in an effort to make it more accessible. These advances have gradually led to the misguided notion that the CLI is an extinct creature from the gruesome past of  computer evolution.

Pretending that the CLI does not exist is perfectly harmless for the average computer user. I would even go as far as recommending it. However, any power user or software developer who avoids the CLI is doing himself a huge disservice. I should know, I did for the longest time. The CLI opens up possibilities that no GUI could ever provide. It lets you perform long and tedious tasks quickly. It lets you automate repetitive tasks. It lets you work the same way across different flavors of Unix. It lets you get really intimate with the machine.

Geeky

After running Linux as my primary OS for quite a while now, I thought I should share my experience and the lessons that I have learnt growing familiar with the CLI.

Linux is the opposite of Windows in many ways. While Windows prides itself in its user friendliness and accessibility, Linux is unapologetically geeky, preferring instead to be viewed as a lean and efficient powerhouse whose singular mission in life is to get things done. To be sure, many projects have evolved over the years with the goal of making Linux a more accessible, general purpose operating system. The most prominent example is without a doubt the ever so popular Ubuntu Linux by Canonical Ltd. Yet Linux can never quite seem to shed it’s heritage as a programmer’s plaything, and as with Windows, this culture pervades the entire ecosystem. There are many Linux programs with which interaction is only possible through the CLI or clunky, halfhearted graphical user interfaces.

A new Linux power user or software developer quickly discovers that unlike on Windows, there is no wishing away the CLI on Linux. The usual reaction is usually one of disbelief, even indignation. But eventually the cold, hard reality settles. Some take off, whimpering and kicking their way back to Windows. A few brave ones stay. If you  are staying, here are six tips to make your CLI journey less grueling and more fulfilling.

1. Do not attempt to memorize commands

Besides cd and ls, there aren’t many other commands that you should deliberately try to memorize. You should let your subconscious handle everything else. I find it does a pretty decent job of helping you remember the ones you use most. All the rest can always be easily found, even without any external reference. I will show you how, shortly.

2. Understand the anatomy of commands

Some commands appear cryptically long and impossible to remember. They are, so do not try to remember them. But they can be understood. Shell programs are written to do one thing and do it well. They read from standard input (keyboard), perform some processing and write to standard output (screen). Optionally, they have options, which in turn may take arguments. To achieve something more complex, commands may be chained together using pipes(|). Each subsequent command then gets its input from the output of the one preceding it. Input and output may also be redirected to other places besides standard input and standard output, to files for instance, using the greater than and less than characters (<, > and >>). Here’s a simple example:

echo cli is fun | cut -c1-3 >> cli.txt

In this example, we use 2 programs echo and cut. The echo program accepts input from the keyboard i.e. the line “cli is fun” and outputs the same to standard output. This output becomes the input of the the cut command. The cut command has one short option specified, -c, which may also be expressed as the long option –characters. For most modern programs, short options are prefixed with a single hyphen and long options are prefixed with double hyphens. Prefixing a long option with a single hyphen leads to expansion, so that cmd -option expands to cmd -o -p -t -i -o -n -s. The result is 7 different short options for the cmd command. In this case, the -c option takes an argument of the form N-M, meaning cut from the Nth character to the Mth character. The output of the cut command therefore becomes just “cli” and is appended to a file named cli.txt by way of output redirection.

Nearly all CLI commands conform to the above pattern. Some commands may require that you first specify a prompt i.e. a word that gives context to the command. For example, all Git commands such as add, commit and pull must be preceded by the prompt git, e.g. git add, git commit or git pull.

3. Know how to find help

As I already mentioned, you should not try to memorize CLI commands. Many CLI programs ship with detailed manual pages that explain what they do and how they should be used. The CLI provides some very handy programs that let you easily get help from right within the terminal. In my opinion, these are the first commands any CLI newcomer should learn.

type: tells you the type of a given command

whatis: gives you a short description of what a given command does

man: lets you access the entire manual page for a given command

apropos: searches names and descriptions of manual pages

help: display help for shell builtins

4. Use aliases

Much of the power of the CLI comes from the fact that simple commands can be chained together to do more complex things. After using the CLI for a while, everyone finds there are commands that they type often. Such commands, when long or difficult to remember, make good candidates for aliasing. Aliasing lets you invoke a command or chain of commands by typing a shorter, more memorable name. For example, to print my wireless LAN IP address I use:

ifconfig wlan | grep inet | grep -v inet6 | awk ‘{print $2}’

This is obviously unwieldy and unnecessary to memorize, so I have it aliased to a much shorter  name: ipad. I also have aliases for Git, Maven, SSH and many other commands that I run frequently.

5. Create functions

Sometimes it is the case that the set of commands you need are executed one by one rather than chained together and therefore cannot be aliased. Not a problem. Such commands can be defined inside a function and then the name of the function becomes the means by which to invoke them all at once. Shell functions are easy to write, even for people without much programming experience.

6. Write shell scripts

Tasks requiring more complex logic are best implemented in shell scripts. Shell scripts are fully-fledged computer programs designed to run in the shell. They can contain control structures and break down big tasks into functions that call each other to implement very complex logic. At the very basic level however, shell scripting is just yet another way of assembling simple programs in interesting ways to solve a big problem.

There you have it. Go on and have fun with the CLI. As a bonus, here are link to my favorite website for learning the Linux shell.

Related articles

5 Functions of Programming Jokes

computer-joke

There are 10 kinds of people in the world: those who understand binary and those who don’t. This is one of my favorite programming jokes. I’ve always enjoyed good jokes. You probably have, too. But did you know that besides tickling your funny bone, programming jokes can also provide a lot of practical value? In this post, I will share with you 5 uses I have found for programming humor.

1. Pique your interest

Every once in a while you come across an incomprehensible programming joke. Sometimes it’s about something you are familiar with, sometimes it is about something you’ve heard about but have no experience with. Sometimes, it is about something completely new. Regardless, nobody likes to miss a joke. So you’ll normally fire up your search engine in your quest to get it. And that leads to one article, then another, then another. In the end, you are so much the wiser not only about the joke but also about its subject in general. If it fascinates you enough, you might make a note to read some more later on – maybe even code something up as a result!

2. Evaluate your skill level

This might sound far fetched, but the proportion of programming jokes that you actually understand does say something about your skill and experience as a programmer. The more jokes you don’t get the less skilled and experienced you likely are. And that’s okay, but it should also serve as a challenge that there’s so much more out there that you need to learn and experience. If you find that you understand nearly every joke you are reading, broaden your humor catchment. Maybe you are just looking at jokes from the one technical area that you are really good at. How about you see what jokes they have for Unix, if you are a Windows developer, for example?

3. Learn a new technology

Maybe you want to start learning C, or Java. And yes of course, you want to start by looking at how they write their classic “Hello world” program – it’s often an excellent way to get a “feel” of any new programming language. But jokes are a great way too. If you already have a strong programming background, a lot of the language-specific jokes will easily make sense to you. They’ll also tend to humorously highlight the nuances and idiosyncrasies of the language – which is just what you need to know before latching onto a new technology.

4. Discover new tricks

Many jokes derive from some of the more obscure elements of a programming language, style or paradigm. So even though they might relate to a technology you are familiar with, they typically raise interesting perspectives from which you may examine the same things. This effectively leads to new knowledge and renewed interest in the possibilities of the subject at hand. “You mean you can do that?”.

5. Unwind

Every programmer will tell you that they consider their career a highly rewarding labor of love. But even passion and enthusiasm run out – eventually, or fatigue just gets the better of you. Which is just as well because we need an excuse for that late afternoon beer. Sometimes, though, it’s 10 in the morning and you can’t seem to get your head to think. A trip to the bar at that hour has the very real potential to be turn the day into your last one at your company. Yet you need an instant escape route from the drudgery of algorithms, data structures and overly complicated programmer tools. No problem. Just head over to your favorite programming jokes website and a laugh your sorrows away.

What other benefits do you derive from programming humor?

PS: This is probably true of all kinds of “professional humor”. As a bonus, here’s a link to some of my most favorite programming jokes on the internet.