3

I wrote the following code in the bash shell. It's supposed to take the positional parameter and, if it starts with a dash "-", print an error message. For some reason the if statement always gets skipped. It only works if I literally input -*.

I get the impression that the fix has something to do with the $.

EXECNAME=$1
if [ "$EXECNAME" = "-*" ]; then
       echo "error: invalid executable name"
fi
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Rami Chaouki
  • 33
  • 1
  • 1
  • 3

3 Answers3

6

You can use double square brackets [[ and ]] without use of quotes on matching pattern for glob support in BASH:

[[ "$EXECNAME" = -* ]] && echo "error: invalid executable name"
anubhava
  • 761,203
  • 64
  • 569
  • 643
0

For matching against glob pattern I prefer to use case:

case $EXECNAME in
  -*) echo "error: invalid executable name"
esac
holygeek
  • 15,653
  • 1
  • 40
  • 50
-1

You didn't mention exactly this case, but I noticed today that the built-in test primaries that are part of Bash's [[ … ]] construct don't perform filename expansion. (It's clearly documented in the manual section 3.2.5.2 Conditional Constructs—I just never noticed.)

What does it mean?

If my current working directory contains the files foo, bar, and go.mod, then

if [[ -f go.* ]]; then
  echo "yes"
else
  echo "no"
fi

will print no.

But

if [ -f go.* ]; then
  echo "yes"
else
  echo "no"
fi

will print 'yes'.

I was trying to see if at least one file with a given prefix existed in the directory, e.g.

for prefix in 1234 2345 3456; do
  if [[ -f "${prefix}"* ]]; then 
    echo "yes"
  else
    echo "no"
  fi
done

But that didn't work. Using the built-in [ command gave the result I wanted.

Colin Fraizer
  • 532
  • 4
  • 14
  • I don't see the relevance to the question. The OP is not trying to use file globs, nor are they using `-f`. – John Kugelman Sep 15 '22 at 04:10
  • 1
    `[ -f go.* ]` is a bad idea because it will error out with `too many arguments` if `go.*` matches multiple files. – John Kugelman Sep 15 '22 at 04:12
  • Re: relevance. Fair enough. I ended up here because I was surprised that my use of a wildcard was failing. I thought my experience might help someone else. Re: "too many args". Very true. In my case, I knew that there would be 0 or 1 file for each prefix. – Colin Fraizer Sep 15 '22 at 13:53
  • A good way to share your knowledge would be to reframe it as a question and answer. See if you can find that question on the site; if so, post your answer. If not, you can post your question and self-answer it. See: [Can I answer my own question?](https://stackoverflow.com/help/self-answer) – John Kugelman Sep 15 '22 at 21:49
  • You did read the title of this question, right? "Using wildcards in bash if statement". I guess I disagree that my answer about `if [[ -f "$prefix"* ]]` is irrelevant, since it is about the use of `*` in a Bash `if` statement. – Colin Fraizer Sep 16 '22 at 02:12
  • You have to read the entire question. It's not appropriate to post an answer that matches the title but has nothing to do with the body. Your answer does not explain what's wrong with `if [ "$EXECNAME" = "-*" ];` nor how to fix it. – John Kugelman Sep 16 '22 at 02:35
  • The question you are looking for is: [Test whether a glob has any matches in Bash](https://stackoverflow.com/questions/2937407/test-whether-a-glob-has-any-matches-in-bash) – John Kugelman Sep 16 '22 at 02:37