Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I recently wrote the following bash function:

makeaudiobook () {
for f in $1; do
        preprocess $f > $f-preprocessed
        text2wave $f-preprocessed -o $f.wav
        lame -b 16 --tt $f --ta $albumartist --tl $albumartist --tn $count $f.wav $f.mp3
        rm -rf $f.wav $f-preprocessed

It does not work as expected. It does not iterate over all of the files that fit the glob expression I specify in $1. As a one-liner, this would work. An in fact, I made this into a function because I was tired of writing it out as a one-liner. But as a bash function, it doesn't work. So what do I need to do to make it work?

I want to be able to call it on things like the output of split, like,

me@localhost$~ makeaudiobook x?? haskell

...where x?? matches xaa, xab, xac, etc. Like I said, as a one-liner this is fine. So what do I need to do to pass this glob expression to $1?

share|improve this question
up vote 11 down vote accepted

x?? is expanded at the time of function call. So your function is already called with xaa xab xac... .

The simplest way would be to change the ordering of your parameters:

makeaudiobook () {
for f in "$@"; do
        preprocess "$f" > "$f"-preprocessed
        text2wave "$f"-preprocessed -o "$f".wav
        lame -b 16 --tt "$f" --ta "$albumartist" --tl "$albumartist" --tn "$count" "$f".wav "$f".mp3
        rm -rf "$f".wav "$f"-preprocessed

(look how I put " around every variable - this is to prevent bad things from happening if you have whitespace in your file names, what often happens with audio files)

and call it with makeaudiobook haskell x??, i. e. with rotated arguments.

share|improve this answer
it works! it works! – ixtmixilix Feb 28 '13 at 9:07
Nit: in "$@" is redundant. for <...>; do by default loops over the parameter array. – l0b0 Feb 28 '13 at 10:23
@l0b0 Right (just forgot about that), but IMHO explicit is better than implicit. – glglgl Feb 28 '13 at 10:51

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.