Skip to content

Instantly share code, notes, and snippets.

@sg-s
Last active December 21, 2015 17:54
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 sg-s/12c56b37cf9c4a3bdca6 to your computer and use it in GitHub Desktop.
Save sg-s/12c56b37cf9c4a3bdca6 to your computer and use it in GitHub Desktop.
A collection of various bash tips and tricks collected from various sources.

The Bash Guide

A collection of various bash tips and tricks collected from various sources. I'm writing this as I learn new stuff, so this is in no way authoritative or even corrent. In fact, you shouldn't read this.

Basics

Version, updates and installation

Check your version:

echo $BASH_VERSION

If you're on Mac OS X, and you're using using homebrew then:

brew install bash
brew update
brew upgrade bash

The path

bash stores something called a PATH variable, telling it where to look for programs and scripts. You can see what your path is by typing:

echo $PATH

You'll get a long list that looks something like this:

/usr/local/bin:/bin:/usr/sbin:/sbin:/usr/bin:/usr/local/go/bin

That's each path, for example /usr/sbin, separated by a colon (:)

Pipes

The $ sign

.bashrc and .bash_profile

Command History

Search previous commands using arrow keys.

Add this to your ~/.bash_rc file

# supports history search using up and down arrows
bind '"\e[A": history-search-backward'
bind '"\e[B": history-search-forward'
bind '"\eOA": history-search-backward'
bind '"\eOB": history-search-forward'
set show-all-if-ambiguous on
set completion-ignore-case on


A handy shorcut to run the previous command with sudo

Add this to your .bashrc file:

# run previous command with sudo 
alias fuck='sudo $(history -p \!\!)'

Programming

Bash programming is horribly hard, and it's easy to make mistakes. But don't let that put you off -- it's a lovely feeling to write a code snippet that you can directly run in your terminal.

Variables

For loops

Matrices, Arrays and Vectors

Misc. Useful programs

Compress all subfolders in a folder to their own zip file

 for i in *
> do
> [ -d "$i" ] && zip -r "$i.zip" "$i"
> done

Sources

  1. Bash Productivity Tips
  2. The Terminal
  3. Defensive Bash Programming
  4. Bash Strict Mode
  5. Updating bash
  6. Upadting PS1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment