Bash brace expansion

The bash shell is so insanely deep that I find it impossible to just sort of learn everything about it at once... so I've been trying slowly internalize commands one at a time - both the shell and the various standard Unix apps as well. Once one or two tricks become sort of second nature, I try to make sure that the next time I see a tips and tricks post online, that I grab something from it to learn again.

Recently I've finally memorized/internalized find's odd syntax (find. -name 'blah') rather than finding and piping to grep. Also I've gotten used to using grep with the -Rin flags to recursively look for case-insensitive text within a directory, outputting the line number. Great for trying to find a function reference in a PHP project that you're not familiar with. And also I've gotten used to using Control-r on the bash command line to repeat previous commands - type Ctl-r and then type the first few letters of what you're looking for, say 'apt', then the last time you used that will come up. Here's the real trick though, hit control-r again, and then one before that will come up, and so on. Took me a while to realize that one. :-)

Remember, what I'm describing is not the greatest bash tricks ever, but just the ones that I've finally memorized to the point where I use them without having to think about it. I don't use aliases much, as I've gotten caught a few times working on a new system thinking that some alias that I used regularly on another computer was a standard thing. I'm trying to memorize stuff that's common - so I don't have to mess with .bashrc files all the time.

Anyways, after that long intro, I was just reading this post at with some bash shell tips and found one that should come in handy... These two commands are equivalent:

$ cp filename filename-old
$ cp filename{,-old} 

As I'm always moving around various files, backing up stuff, trying out different versions, this might be very handy to remember. What's happening is that bash is combining the word before the braces with each element within the comma separated list. Normally I just use tab completion with copies and moves. But when it comes to log files, which can be long, and named similarly, this can be a bitch... in those instances where I've typed out a really long path/filename, etc. and it'll be a pain to retype, this will work *really* well I think.

That's it. Just the one tip. The article has a lot more, but if I try to learn them all, I won't. However, if there's a bash trick you have that you use every day and just can't live without, please send it to me!



< Previous         Next >