Server Fault is a question and answer site for system and network administrators. 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 got a shell variable holding this path


There COULD be a file named bitmap.1, but it could also be bitmap.1.z, bitmap.1.gz, bitmap.1.Z.

Now I want to pass the real file to a function while at the same time resolving the correct file, like

function process {
    # do something


process ${path}.(z|Z|gz) # here the shell shall resolve the real filename

Is there a way to do it elegantly or to I have to test if bitmap.1, bitmap.1.z etc. exit and then pass that file to the function???

Forget to mention I'm restricted to the Bourne-Shell, so no Brace Expansion at hand -,-.

share|improve this question
up vote 1 down vote accepted

You can still do the globbing in Bourne Shell

for f in ${given-path}*; do
process $f

It's a good idea to do as much of your shell scripting as possible limiting to Bourne shell syntax; that maximizes portability. In many cases, it also improves speed.

share|improve this answer

If you know the list of possible extensions, you can do:

process ${path}{,.Z,.Z,.gz}
share|improve this answer

I believe Christian is on the right track. If you are certain of at least a portion of your path, using Bash's builtin globbing will yield any files matching your pattern.

For example, I have a function in my .profile which I frequently use to cat gzipped and plain-text log files:

function mscat {

  for file in $@; do
    if [[ "$file" =~ $is_gzip ]]; then
      zcat $file
      cat $file

Which I would call like this:

mscat /var/log/ncftpd/sess.2010*

Because globbing can yield more than one result, your 'process' function should be capable of acting upon multiple arguments. In my example, you can see how I do this by iterating over each argument in $@.

share|improve this answer

when you only have one file you can use bash globbing process ${path}*

share|improve this answer

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.