Bash one-liner: Copy current working directory to clipboard in OS X

I frequently find myself in need of copying the path to my current working directory. Under OS X, you can pipe the output of any command to the clipboard using the pbcopy and pbpaste (see `man pbcopy`). Easy enough with `pwd | pbcopy`, but you end up with a newline. No problem, `tr` to the rescue.

pwd | tr -d ‘\n’ | pbcopy
^[1] ^[2] ^[3] ^[4]

1. `pwd` prints the working directory; pwd outputs a newline, which isn’t very handy if you need to paste this elsewhere, and especially unhelpful in a shell
2. `tr` is short for translate characters (think find/replace); normally it takes two arguments, but passing the `-d` flag deletes the string passed as an argument from the stdin
3. `\n` is the escape sequence for newlines; the character we want to get rid of
4. `pbcopy` is the OS X bash clipboard tool; see also: xclip or xsel for X-Windows environments

Use `man ` for more details about any of the above.

I’ve added the following as a bash alias so I can call this one quickly:

alias cpwd=”pwd | tr -d ‘\n’ | pbcopy”

Easy!