The Raspberry Pi was invented for learning how to program. You can load up nearly any language imaginable and the relatively low price of the board puts the technology within reach of everybody. Hook up a decent power supply, keyboard and mouse along with a cable going to an HDMI big screen and you are off and running.
As off-the-shelf hackers we integrate mechanisms, hardware, software, techniques, processes, aesthetics and tools into physical computing prototypes and interesting projects. As such our programming needs are modestly confined to fairly simple user-facing programs and microcontroller firmware. Powerful applications, portability and tools are what matter to us.
Linux is a powerful toolbox built into every Pi. Open the lid and there sits the text-based command line, one of the most useful tools on the Pi.
I started a new series of stories introducing the command-line a couple of weeks ago. We covered ls, cd, pwd and cat. This week we’ll look at some more commands and a tiny bit of the design philosophy that makes the command line so effective.
The Text Flies By
As you may recall, the cat command prints a file to the screen. Unfortunately, larger files scroll by so fast, you simply can’t read them. Wouldn’t it be great to be able to read a page at a time and move up or down through the file? Why not use the more command?
Type “more” and the file name at the command-line and your wish will be granted. Advance to the next page with the space-bar. Want to go back up a page? Type the letter “b”.
Notice that using the space-bar and “b” are a very logical way of organizing the behavior of the command. This is part of the “Unix philosophy” of how things are done. Unix was originally a terminal-based system and used the keyboard for input. There was no graphical user interface, high-resolution display or even a mouse. Back then programming was very tedious, so anything that could be done to speed up inputting code, testing and debugging certainly helped the workflow. The “b” metaphorically means “back,” is also right above the space-bar and can be easily typed from the standard home QWERTY keyboard hand position. Well, that is if you can touch time. Linux carries on many of the old-school Unix traditions because they make sense and just work.
I learned to touch-type in junior high school, never even imagining that I’d use it extensively years later as an IT pro and technology writer. Don’t ever discount those strong hunches about possible future tech opportunities, even if you haven’t yet connected any dots.
cat and more are constantly used on my command line, whether on the Pi or the Linux notebook.
Another way to speed up working on the command line is to simply type less.
Suppose you have a complicated command line with a bunch of options and a fairly long file name. Maybe you need to run the command multiple times. Retyping the same command each time is horrendously tedious and errors are practically guaranteed.
Linux captures your commands, as you type and stores them in a sequential queue. At a blank command line simply hit the up-arrow key to step back through your commands one at a time. Type the “Enter” key to execute the desired command when it appears. Once you are up in the queue, you can use the down arrow key to move back toward your most recent command lines. Get completely out of the queue and be ready for a new command by using the control-C combination. Then, type your new command. Naturally, it will be pushed into the command-line queue as the most recent one.
By the way, you can also edit the recalled command using the right and left arrow keys, along with the delete and insert keys. This capability comes in handy when you want to change a few letters or numbers and run the command.
The command-line queue is also accessible through the history command. Instead of stepping back through two-hundred commands with the up-arrow, type “history” at the command-line then use the scroll bar or mouse-wheel in the terminal window to find the command you want. Click the maximize icon, at the top of the terminal window to get a full display of previous commands.
When you’ve found the command line you’d like to run, look to the left edge of the screen and note the line number. Type a “!” character immediately followed by the line number to execute the command. It might seem like a lot of work, but it really is efficient once you get used to the process.
One oddity with history is that as soon as you type the “!” and line number followed by the Enter key, the command will execute immediately.
Linux has a bunch of command-line programs, that can be leveraged on the Raspberry Pi. We usually think of the Pi as an Internet of Things-type device. I think it is much more. It’s a full-blown Linux machine on a board a little bigger than a credit card. Explore that command line and see what you can do.
Catch Dr. Torq’s Off-The-Shelf Hacker column, each Saturday, only on The New Stack! Contact him directly for consulting, speaking appearances and commissioned projects at firstname.lastname@example.org or 407-718-3274.