2

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 Answers 2

4

The last line prints what really is in $firstfile.

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

3

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"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .