1

I'm trying to grab the first file matching a pattern and use that in a statement. Using the same steps as found in How can I get the first match from wildcard expansion? I wrote the following:

#!/bin/bash
files=("*.sql")
firstfile=${files[0]}
echo $firstfile
echo "The first file is $firstfile"

When I run this, the output is:

sqlfile.sql

The first file is *.sql

Why does the value of $firstfile change based on the context?

2

Take out the quotes in the files line, which leaves:

#!/bin/bash
files=(*.sql)
firstfile=${files[0]}
echo $firstfile
echo "The first file is $firstfile"
3

The last line prints what really is in $firstfile.

The first echo expands $firstfile via the filesystem as you did not use double quotes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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