Command Line Background

Software to download and install:

Windows:

Git Bash: https://gitforwindows.org/

MinGW might also have some additional packages you might want to install. MinGW was covered in the C++ chapter, but aside from C++ packages, there are other things you can install in it.

You can also use PowerShell, which comes with Windows, but isn’t very good in my opinion.

Optional:

ConEmu: https://conemu.github.io/

You can use Git Bash and PowerShell within ConEmu.

macOS:

Homebrew: https://brew.sh/

Within a terminal window, you might want to run these commands:

brew install coreutils

brew install moreutils

brew install binutils

brew install htop

Optional:

iTerm 2: https://www.iterm2.com/

Linux:

No additional software is required, as Linux distros come with a lot of stuff already. However, there are a few optional things you might want to install:

Optional:

Terminator:

Within a terminal window, type this:

sudo apt-get install terminator

htop:

sudo apt-get install htop

Basic command line skills – The command line can be potent, but many people are used to graphical computing these days – menus, buttons, pictures, and other visual indicators. But you’d be surprised at just how useful and fast many command line things are. It’s worth knowing at least the basics. This chapter contains a lot of Unix-centric command line tools you should be aware of.

Linux comes with a lot of great command line utilities by default, so you don’t need to install much if you’re a Linux user. You can use GitBash and MinGW on Windows to get a similar experience. If some things are missing on macOS, you can use the homebrew package manager to install extra command line utilities, such as using the following commands:

brew install coreutils

brew install moreutils

brew install htop

brew search whatever

The Windows command prompt and even PowerShell lack a lot of the commands I list below. That is why I recommend GitBash for Windows, possibly with ConEmu and MinGW. You can try using Windows command line stuff without it, but your experience will be severely hindered.

macOS’s homebrew package manager doesn’t come with it by default, so it needs to be downloaded and installed separately. You can use a terminal and a shell in macOS without it, but it’s a very limited experience compared to Linux. Speaking of defaults and macOS, the default shell in macOS used to be Bash, but Apple is changing it to zsh instead. But they still have a lot of overlap.

Once you grok the command line, you will be a much faster and more efficient worker than before. You’ll wonder how you got by without it.

Terminal – a way to interact with a computer using text commands as opposed to buttons and graphics. Technically, nowadays we only use terminal emulators, not actual terminals. Back in the day, people used dumb terminals to connect to remote servers because computers were too expensive for the average person to own. But nowadays, computers include terminal emulators because text commands are still powerful, but we have actual computers to run them on instead of terminal hardware.

Shell script – terminals use shells. Shells allow you to enter in text commands, one line at a time. A program you write might be many lines of code, but a shell command is like coding with only one line. There are plenty of shell one-liner commands that can do a lot of powerful things, but you can take it one step further and write a shell script, which is a collection of shell commands to do a more complicated task that isn’t suitable for a single line command. It’s somewhere in between command line stuff and a more fully-fledged program. Usually, shell scripts are convenient and reusable, but not as complex as a program you’d write in a “real” programming language. Some examples of shells include Bash, zsh, and PowerShell. I’m honestly not a fan of PowerShell, and Windows 10 has better support for Bash, so I’d recommend that. Some IT people can do shell scripting, but not object-oriented programming – usually because their job doesn’t require it.

Shebang – for shell scripts and python, you can use a shebang at the beginning to tell the computer how to run it.

A shebang for a shell script might look like this:

#!/bin/bash

Or this:

#!/usr/bin/env zsh

Then you write your script on the lines after that.

As long as you have a shebang, you don’t even need a file extension – at least for bash and Python. It might be different for other languages though. Sometimes people name shell scripts as something.sh, but you don’t need the .sh file extension for the computer to know that it’s a shell script.

A python shebang might look like this:

#!/usr/bin/env python3

In that case, it’s using an environment variable rather than a hardcoded path, because people might have Python 3 installed in different locations.

If you run Python with the python or python3 command line command, or from an IDE, you might not need the shebang. Even so, it’s good practice to use them regardless.

Command line arguments – when you run a command line program, you can pass arguments to it, just like with functions within a program.

Here is a program being run with no command line arguments:

./my_multiplication_program.py

The output might be something like this:

Error: missing arguments

Here is a program with two command line arguments (5 and 6):

./my_multiplication_program.py 5 6

The output could be this:

30

It’s a way to take user input and pass it into the program. There are other ways of getting user input though.

Command line arguments aren’t just for scripts you write. They’re also widely used by command line utilities.

For example, in the command ls -a, ls is the program and -a is an argument passed to it.

Dotfiles – there are lots of configuration files on a computer, for things like vim (.vimrc), a git repository (.gitignore), bash (.bashrc and .bash_profile), Ruby (.gemrc), ssh (~/.ssh/), zsh (.zshrc), etc. As you can see, all these config files start with a dot, hence “dotfiles.” Some people maintain a repository of their dotfiles to make it easier to transition to a new computer. That way, you can get your programs with the settings the way you like them. Not all software supports dotfiles though, although you can take a snapshot of your computer’s software with something like a restore point (in Windows) or use Clonezilla or Acronis to make a disk image, which saves your software states as well as files. Some modern alternatives to dotfiles include programs which let you sign in and sync settings. One example of this is Firefox Sync, which enables you to sign in to Firefox on a device you haven’t set up yet, and then it automatically configures your browser with your settings, add-ons, bookmarks, and more from your other device.

However, that easy setting syncing feature won’t win you any geek cred. Some techies actually like the esoteric nature of dotfiles, along with other types of config files. This isn’t entirely related to dotfiles per se, but you will find that some programmers or security researchers prefer counterintuitive software because they think it makes them look smarter. You could just use a JetBrains IDE that would be useful right after installing it… or you can use vim and spend hours customizing it until it’s finally good, then learn all the ins and outs of the keyboard-centric navigation.

Anatomy of Unix-based operating systems

User – an account that can run commands in a limited way. The area of limited privileges that a user has is called user space. Programs that are made for the user to use, as opposed to system tools for the kernel, are called userland software.

root – a special user account with extra privileges. Can do pretty much anything on a computer. Don’t use root unless you absolutely have to. A type of security vulnerability where a user can elevate themselves to root privileges is called privilege escalation. Privesc is a common thing in hacking.

Non-root user account – a user that is not allowed to do just any old thing. This is safer. Even if you own your own computer, you should not be root unless you really need to. This is because it’s less secure and easier to make serious mistakes as root.

Shell – a shell lets the user interact with the kernel using syscalls.

System call a.k.a. syscall – a way for a user to interact with the kernel in a kind of indirect way.

Kernel – stuff with extra privileges that allow the system to do more advanced stuff that might not be safe for a user to do, regarding memory, processes, and whatnot.

Device drivers – how the computer talks to hardware, such as printers, network cards, graphics cards, and more. Some drivers need to be installed separately, but others come with the operating system. A device that can be plugged in or removed when the computer is turned on, and which often work right away, are called “plug ‘n’ play” or PnP. A keyboard is plug ‘n’ play, but a graphics card is not.

Hardware – the physical component of a computer.

A user uses a shell. A root user can do more things in a shell than a regular user. Shell utilities are programs you can use at a shell. A shell uses syscalls. A syscall interacts with the kernel. The kernel uses device drivers. Device drivers use hardware.

← Previous | Next →

Command Line Topic List

Main Topic List

Leave a Reply

Your email address will not be published. Required fields are marked *