Channels ▼

Mark Nelson

Dr. Dobb's Bloggers

The Never-ending Awesomeness of Bash

October 17, 2010

This semester I'm teaching a class on Linux/UNIX, and am enjoying it immensely. With every lecture I'm reminded that you simply never stop finding new tools and tricks to use in an O/S that is now well into middle age.

One of my midterm questions from last week was a basic query regarding filename expansion under bash:

Write a command to copy files chapt00.txt, chapt01.txt, through chapt15.txt from the current directory to your home directory. Make the command as short as possible.

The answer I was expecting was
cp chapt0[0-9].txt chapt1[0-5].txt ~
and that is indeed what I got from the majority of the students. Of course a few were confused about the use of character classes and tried to get something like chapt[00-15].txt to work. But one student turned in something a bit more novel:
cp chapt{0[0-9],1[0-5]}.txt ~

My initial reaction was that this was simply a misguided attempt at filename expansion using broken syntax. But I was assured that this worked properly, and a quick look at the bash reference manual showed me the error of my ways. It turns out I was completely overlooking a nice feature of the shell: brace expansion. When the shell encounters a sequence of the format preamble{comma-separated-list}postamble, it iterates through the comma list of tokens inside the brackets and generates a new string for each one. The GNU manual example is for the sequence a{d,c,b}e, which generates a space separated list: ade ace abe.

What this means is what I took to be an incorrect file specification actually generated the sequence chapt0[0-9].txt chapt1[0-5].txt. Since brace expansion occurs before filename expansion, this results in the desired output. So this answer, being more terse than my expected response, is actually more right.

Our machines at school are using bash 3.2. If we upgraded to bash 4, we could use a numerical sequence with brace expansion to get even a better answer. The expression chapt{00..15}.txt should expand to the list of all 16 file names. But bash 4.1 is still only slowly working its way into new distributions, so it may be a while before we can count on that syntax to work everywhere.

The moral of the story, of course, is that no matter how much time you spend working with the UNIX/Linux command line, there is always plenty more to learn. I should be paying UTD to teach this course, not the other way around.

Related Reading

More Insights

Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.