Table of Contents

Command-line Ninjary GSPS 2013

This is meant to be a quick reference guide to command-line usage for those of us who have the basics down, but aren't yet true ninjas. Please add your own tips or quick examples! We spend a surprising amount of our lives inside a terminal, so learn to love it and use it well. Note: this page was written for bash, but much of this stuff should translate to csh or your shell of choice.


General notes about navigation commands: First, “Ctrl” commands are the same as in the text editor emacs, so you may find that other emacs line navigation commands work as well. Second, if you are on a Unix machine, note that the line navigation commands work almost anywhere you can edit text - you will learn that accidentally if you start using these commands compulsively. Third, if you use “Ctrl” commands often, you may consider setting the “Caps Lock” key as an additional “Control” key - this is just a computer setting for Unix (e.g. System Preferences > Keyboard > Modifier Keys) but requires slightly more work on Windows (e.g. the AutoHotkey program).


Really Useful Built-ins

Get A Package Manager

Best way to install and manage command-line tools. Native for all linux builds; have a couple good choices for macs.


Pipes & Redirects

Invoking multiple commands

Piping stuff around

Useful tools when piping

Loops & Functions

Variables

Variables that have already been declared are prefaced by a $, but you don't need one when first declaring a variable. Any variables declared normally (x=4) are considered strings, to declare a number use the let keyword: let x=4. You can also declare a variable to be a command by wrapping it in back-ticks or parentheses. I.E. LIST=(ls); $LIST. There are some system-wide variables that should always be available, like $SHELL, $PATH, and $HOME. Note that though convention says system-wide variables should be all caps, they don't need to be.

Simple Loops

There are several ways to write loops, the most useful of which are below. Note that in bash an $ always indicates a variable, and that all variables are, by default, treated as strings. The actions to be repeated must be wrapped in do/done statements, and the semicolon marks an end-of-statement (equivalent to a line return). A few examples:

for i in {1..20}
  do
    echo $i
  done
for i in {a,b,d,g}; do echo $i; done
for i in a 2 b 6;
  do
    echo $i;
  done
for file in *filePattern*;
  do
    echo file;
  done

Simple Tests

The shell can do arithmetic and logic tests, but I think if you're doing anything very complex you should move into Python or Perl. Logic comparisons should be put in square brackets (spacing is important), if statements must be closed with a fi, and the operators you have at your access are:

Use more aliases!

Use aliases to make hard-to-remember commands less terrible.

Example: http://xkcd.com/1168/:

Defining your own functions

You can declare functions and use them at any time. The most common way to interact with functions is through required arguments, as shown below. Adding in flags (like -v) for optional arguments takes some more work (look up getopts).

Some examples:

function speak () {
  echo here are all $# arguments: $@
  echo here is argument 1: $1
  echo here is argument 3: $3
}
function killme () {
  ps -e | grep $1 | awk '{print $1;}' | xargs kill
  echo $1 is dead
}
function cls () {
  cd $1
  ls
}
.profile or .bashrc

There are a set of system files in your home directory that are executed every time a terminal is opened - use those to define aliases, functions, or variables you want always available.