There’s lots of neat things that can be done with a bash terminal. Here are some tricks that I’ve found over the years.


Some of these recipes might use some fairly standard linux tools on the bash command line rather than bash itself.

Redirecting Output

  • Stdout is stream 1, stderr is stream 2
  • redirect one stream at a time with 1> or 2>.
  • redirect one to the other with 1>&2.
  • redirect both using &>.

awk: Tips and Tricks

awk ' !x[$x0]++' <file>
Read from <file> (use "-" for stdin) and remove any duplicate lines without changing order.

Determine Location of the Script being Executed

Basic Form:
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
SymLink Friendly:

DIR="$( cd -P "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

Only works if symlink is not last component of the path

Most Robust:

SOURCE="${BASH_SOURCE[0]}"
DIR="$( dirname "$SOURCE" )"
while [ -h "$SOURCE" ]
do
    SOURCE="$(readlink "$SOURCE")"
    [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE"
    DIR="$( cd -P "$( dirname "$SOURCE"  )" && pwd )"
done
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
" "