How do I deal with spaces in filenames for a wildcard parameter in a bash script?

24 views Asked by At

I am writing a bash script that will take a list of filenames as an argument. It will generally be used with wildcards.

Right now, the script just prints the files:

#!/bin/bash
for file in $@
do
    echo "Processing $file" 
done

The problem is when using a wildcard, and the file list has filenames that contain spaces. For example, if I have two files file1.jpg and file two.jpg, and I run ./myscript.sh *.jpg my output is

Processing file1.jpg
Processing file
Processing two.jpg

How do I make it recognize "file two.jpg" as one thing instead of two things? Note that the wildcard could be just * and files are not guaranteed to have an extension.

I am running this on a Mac, using GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin21)

0

There are 0 answers