Skip to content

Instantly share code, notes, and snippets.

@scottgonzalez
Last active August 1, 2017 19:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scottgonzalez/131bf444fd79795d221aa953214a1c5a to your computer and use it in GitHub Desktop.
Save scottgonzalez/131bf444fd79795d221aa953214a1c5a to your computer and use it in GitHub Desktop.

Shell

The shell is a layer above the kernel and can be a command-line shell or a graphical shell. We're going to focus on command-line shells.

The Shell Command Language is a POSIX standard which has many implementations. Not all shells have the same functionality, so it's important to know what is common amongst all popular shells and what is unique to specific shells. sh, bash, csh, zsh are some common shells.

Manual

Shells include online manual pages, often referred to as man pages, due to the man command which displays them.

  • man man displays the manual page for the man command.

Built-in commands

  • man builtin displays the manual page for the common built-in methods.
  • As mentioned before, the built-in methods are shell dependent.
  • Examples of built-in methods:
    • alias - basic string substitution for commands
    • env - display/create an environment
    • export - set a environment variable
    • kill - send a single to a running process
    • popd/pushd - traverse directories using a stack
    • pwd - print the working directory
    • source - read and execute a file in the current shell context
    • time - measure how long a command takes
    • unset - remove variables and functions

Other common commands

  • cat - concatenate and print the content of files
  • cd - change working directory
  • grep - search files for text
  • head - print the beginning of a file/stream
  • less - print content with pagination
  • ln - link files
  • ls - list files
  • sudo - execute a command as another user
  • tail - print the end of a file/stream

OS X only

  • pbcopy/pbpaste will copy/paste content into/from the clipboard

Multiple commands

  • command1; command2 - Run command2 regardless of result of command1
  • command1 && command2 - Run command2 only if command1 is successful
  • command1 || command2 - Run command2 only if command1 is not successful
  • command1 | command2 - Pipe the output from command1 to command2

How do we know if the command was successful? We can check the exit code.

Exit codes

  • 0 indicates success, while any other number indicates an error.
  • Exit codes are specific to the command being run.
  • $? contains the exit code for the previous command

History

Shells have tools for working with the history of commands that have been run.

Rerun the most recent command

To rerun the most recent command, there are a few options:

  • UP will scroll through the history in reverse chronological order.
    • DOWN will scroll through the history in chronological order.
  • CTRL+P does the same as UP (P = previous).
    • CTRL+N does the same as DOWN (N = next).
  • !! will execute the previous command again.

Traversing history

What if the command you want to rerun is older?

  • history will show you a list of all stored previous commands.
    • HISTSIZE controls how many entries are stored.
    • HISTTIMEFORMAT controls the time formatting for when a command was run.
      • Try HISTTIMEFORMAT='%F %T ' history.

Any previous command can be executed based on its position in the history:

  • !{number} - The command identified by {number}.
  • !-{number} - The command run {number} commands ago. (!-1 = most recent command)

Appending :p to one of the ! history commands will print the command rather than executing it. The expanded command will be placed into the history again.

Previous commands can also be searched by prefix:

  • !ls will rerun the most recent command that starts with ls.

Reusing parameters

The parameters from the previous command can be referenced individually.

  • !^ - The first parameter
  • !$ - The last parameter
  • !* - All parameters
  • !:{number} - The paramter in position {number}.
  • ^search^replace - Replace search with replace from the previous command

Manually searching through history

reverse-i-search allows you to search through history based on substrings.

  • Press CTRL+R to start searching.
  • As you type, the most recent match will be displayed.
  • Press CTRL+R again to see cycle through matches
  • Press ENTER to execute, or BACK to edit.
  • Press CTRL+G to exit the search.

Creating commands

  • alias - good for simple commands
  • functions - good for longer commands and better for debugging
  • Custom executable files
    • Most languages have the ability to create executables.
    • Identify the interpreter via a shebang line (+1 optional argument).
      • e.g., #!/usr/bin/env node
    • Add the executable to your PATH.
      • Easiest way is to symlink to a bin directory.

User input

It's always a good idea to gracefully handle user input. Don't write scripts that require changes to the source code for multiple runs. Take in arguments as input from the user. This can be done via interactive prompts or via command line arguments.

For command line arguments, use an existing library for parsing and displaying help text.

Colors

Strategic use of colors can greatly improve the output of a program.

Colors are defined using escape sequences in the output. The escape sequence is comprised of four parts:

  1. Escape character - represented by \e, \x1b, or \033.
  2. Control Sequence Introducer - [.
  3. Color codes - prefix, color, and text decoration; delimited by ;.
  4. Finishing symbol - m

Prefixes

Prefixes can expand color palettes to more than just the basic 16 colors, but we'll stick to the basics for now.

Color Codes

  • Foreground
    • 30 - Dark Gray
    • 31 - Red
    • 32 - Green
    • 33 - Yellow
    • 34 - Blue
    • 35 - Purple
    • 36 - Turquoise
    • 37 - Light Gray
  • Background
    • 40 - Dark Gray
    • 41 - Red
    • 42 - Green
    • 43 - Yellow
    • 44 - Blue
    • 45 - Purple
    • 46 - Turquoise
    • 47 - Light Gray

Text Decorations

  • 1 - bold
  • 4 - underline

Custom prompt

The PS1 environment variable controls the prompt string. You can put anything you want into the prompt.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment