Skip to content

Instantly share code, notes, and snippets.

@ArtBIT
Created January 15, 2016 18:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ArtBIT/be59285fe673af16ba09 to your computer and use it in GitHub Desktop.
Save ArtBIT/be59285fe673af16ba09 to your computer and use it in GitHub Desktop.
Bash History Video Tutorial Transcription
  1. Overview of BASH history

    • history
  2. Basic History Recall

    • !! - Previous command
    • !n - Nth previous command
  3. Parameter Recall

    • !* - All parameters from the last command, excluding 0th (the command itself)
    • !:n - Nth parameter from the previous command (index starting from 1)
  4. Advanced Recall

    • !?<search> - Previous command with the word <search> in it, if you want to use a command on such a recall you will need to close <search> with a ? ie., !?foo?
    • !<command> - Previous <command>

From here on any one of !!, !n, !<command>, or !?<search>? will be referred to as <history ref>

  1. Advanced Parameter Recall

    • <history ref>:<n/^/$/*> - Nth, first, last, or all parameters respectively of the <history ref>
  2. Basic Replace

    • ^foo^bar - Replace first occurence of foo with bar in previous command
  3. Advanced Replace

    • <history ref>:s/foo/bar - Replace first occurence of foo with bar in <history ref>
      • Example: !395:s/sbiddle/shawn/
    • <history ref>:gs/foo/bar - Replace all occurences of foo with bar in <history ref>
  4. Modifiers (These are essentially filters applied to a substitution)

    • <substitution>:t - The "tail" of the string, ie., the filename in a path
      • Example: !cat:t
    • <substitution>:h - The "head" of the string, ie., the path minus the filename
    • <substitution>:q - Surround the substitution with single quotes
    • <substitution>:x - Split the substitution on spaces/newlines and then surround those with quotes

Example:

> cat /foo/bar /bazz/quz
...
> echo !cat:*:x
echo '/foo/bar' '/bazz/quz'
  1. History Settings All of these you can set in your .bashrc with export <setting>=<value>
    • HISTSIZE (number) - How many entries to store (defaults to 500)
    • HISTCONTROL (string) - Behaviour of the history
      • ignoredups - Don't put the command in your history if it's the sam as the previous command
      • ignorespace - Don't put the command in your history if there is a leading whitespace. Useful if you want to run some commands without having them be in the bare history file
      • ignoreboth - Both of the previous settings.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment