Introduction to Unix Command Line

The primary mode of interacting with a unix system is using the command line. While it may look like using a graphical user interface is lot easier to use, it is not usually optimal in most of the cases. While the command-line interface may have steeper learning curve, the ability to automate things easily pays off the effort very quickly.

Let me recall the quote that we have discussed from SICP.

Every powerful language has three mechanisms for accomplishing this:

  • primitive expressions, which represent the simplest entities the language is concerned with,
  • means of combination, by which compound elements are built from simpler ones, and
  • means of abstraction, by which compound elements can be named and manipulated as units.

– Structure and Interpretation of Computer Programs

Consider the task of creating a mp4 video from frame images generated by your C program, like some of you have done. It requires running the program, converting each of the SVG image generated by the program into png and combining all of them together to make it into a video.

The typical way of doing this using a graphical user interface would really cumbersome. That may involve doing something like this:

  • Run your program
  • Open the image converter program, select an svg file and press a button to convert it to png and repeat it for each one of the images
  • Open the video maker program, select all the png files that need to be included, press a button to make the video

It is really tedious to do this again and again. If we look this in the lens SICP, the primitive expressions of this system are your program, image converter and video maker. The only way to combine them is by doing each one of them one after the other and there is no way to abstrat them, give it a name and use like a single operation.

When you think of command line, you can take combine the primitive commands by running them one after the other, but you can also abstract them and give it a name by making a script and running just that script.

In this lesson, we’ll learn the how command-line interface works and how to use it effectively.

Quick Setup

Please install the required packages to install the commands used in this lesson.

$ sudo apt-get -y install coreutils figlet cowsay wbritish

Getting Started

Let’s start with priting a hello message.

Open your terminal and type echo hello world. You’ll see the message hello world printed back.

$ echo hello world
hello world

Pipes

One of the beautiful things about Unix command line is the ability to connect the output of one command as input as another one.

We’ve seen two command seq and figlet. Wouldn’t it be nice if we can print the numbers as large characters using figlet? For this we need to connect the output of seq to the input of figlet.

$ seq 5 | figlet
 _
/ |
| |
| |
|_|

 ____
|___ \
  __) |
 / __/
|_____|

 _____
|___ /
  |_ \
 ___) |
|____/

 _  _
| || |
| || |_
|__   _|
   |_|

 ____
| ___|
|___ \
 ___) |
|____/

The | charater creates a pipeline connecting output of the first command to the input of the second command.

Shell Scripts

numbers.sh
n=$1
seq $n | figlet

The $1 is the first argument that is passed to the script.

Remember that in a shell script, space around = are not accepted. So n=$1 is valid, but n = $1 is invalid. We’ll learn more about the nuances of shell scripts later.

And you can run it using: bash numbers.sh

$ bash numbers.sh 2
 _
/ |
| |
| |
|_|

 ____
|___ \
  __) |
 / __/
|_____|

References