Thursday, May 19, 2011

echo Bash{Brace Expansion, Buildin Type}

Today a colleague of mine, Elliot Shank (@clonezone on twitter), introduced me to two new bash commands that I think are pretty useful. I've decided to blog about them in the hopes that the commands will stick.

First Brace Expansion. You can read a nice over view of Brace Expansion here. The summary gives a nice example of using it to make mulitple directories. You can also use it to rename a file that is in a deep directory structure, for example like this:

mv /site/web/hello_world/version1.0.0/data/2010/05/17/foo{,.bar}

So rather than typing out the whole directory structure again you just have to type it once! The above expands to:

mv /site/web/hello_world/version1.0.0/data/2010/05/17/foo /site/web/hello_world/version1.0.0/data/2010/05/17/

Don't forget the ',' in the braces either. Basically the braces are being expanded and the first element is nothing.

The second command is the bash builtin command type. Type doesn't have a man page but you can get information about it by using the built in help and by typing type:

help type
type type

type is an alternative to which. The big difference is that which is an external command whereas type is a bash builtin. You can read more about it here. With type(and which) you can things like vim `type -p foo` which is kind of nice.

In my time tests these were my results:
using type:
real 0m0.000s
user 0m0.000s
sys 0m0.000s

Using which
real 0m0.003s
user 0m0.001s
sys 0m0.002s

So type is a little faster.

No comments: