2

Running this in terminal runs fine:

for a in *.pdf; do mv "$a" "$a".tmp; done

Running this inside a bash script (#!/bin/bash)

for a in "$1"; do mv "$a" "$a".tmp; done

...and passing the parameter *.pdf, i.e. ./myscript *.pdf, the script processes only the first file in the directory. Testing by changing mv to echo shows the same thing.

Any explanation why this is so, and how I could make it work to process all file matches? Thanks!

Majal
  • 1,635
  • 20
  • 31
  • the shell will already expand the `*.pdf` and pass in the list of pdfs to myscript. if you want to pass the string `*.pdf` to your script you will have to quote or escape it. `./myscript "*.pdf"` should work – Kai Iskratsch Jan 22 '16 at 13:07

2 Answers2

2

When you run

./myscript *.pdf

first thing that happens, is that *.pdf gets expanded to all matches. So what you actually call is

./myscript file1.pdf file2.pdf file3.pdf

Try

for a; do
    mv "$a" "$a".tmp
done

This will loop over all positional arguments.

pfnuesel
  • 14,093
  • 14
  • 58
  • 71
2

The problem is that $1 is only the first argument (here the first pdf). To loop through all the arguments, use:

for var in "$@"; do
   do mv "$var" "$var".tmp
done

$@ is a bash built-in variable, an array containing all the command-line parameters. So you know, there is also $*, which is :

All of the positional parameters, seen as a single word (src)

Derlin
  • 9,572
  • 2
  • 32
  • 53