3

Is this a Bash bug?

$ mkdir test && cd test && echo "a" > "some.file"
test$ echo '*'
*
test$ TEST=$(echo '*')
test$ echo $TEST
some.file

Why is the second output the resolution of * into (all) filenames instead of just a literal * output? Looks like a bug in Bash?

Tried on Ubuntu 18.04, bash version 4.4.19(1)-release. Expect it will be the same on other OS'es.

marked as duplicate by muru, Scott, ilkkachu bash Jun 3 at 19:08

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 2
    General procedure when finding bugs in Unix: 1) Check out Single Unix Specification online for relevant definitions and rationales. 2) Submit a bugreport to austingroupbugs.net 3) Submit bugreport to the developer (with a patch if possible). – DannyNiu Jun 3 at 1:10
  • 3
    @DannyNiu: Your comment is not very practical.  Documents like POSIX and the Single Unix Specification do not consist of millions and millions of examples; they contain explanations (with, maybe, a few examples).  Those documents are of little help to somebody who doesn’t understand what is happening and why; reading them is like learning to drive by reading a roadmap. And this question isn’t asking what to do.  If you want to post a question, “What should I do if I’ve found a bug in a widely used software product?”, and post an answer, go ahead. – G-Man Jun 3 at 4:26
  • @G-Man I did find DannyNiu's answer helpful in terms of the information provided, "just in case". – Roel Jun 3 at 4:28
13

No, it is not a bug. You have shown that

echo '*'

will produce a literal *. Hence when you substitute this output, as per the following command

TEST=$(echo '*')

it will put * into the variable $TEST. Then when you

echo $TEST

the glob will expand here. You can verify this by running this last command, changing directories, then running it again.

You will get the * output if you say

echo "$TEST"

as explained here, the double quotes allow the variable to be expanded but prevent the glob from expanding.

  • Aha, I see. Thank you. I missed the fact that the echo * does not have quotes and thus resolves. It's harder to see when you have it inside a complex script :) Makes sense now. – Roel Jun 3 at 1:36
  • 3
    @Roel FWIW, after a few years of experience you will start to treat $TEST instead of "$TEST" as a code smell and it becomes a bit easier to see. It's still not much of a smell though so it will still trip you up sometimes – slebetman Jun 3 at 9:24
  • 8
    And this is why my general approach is, if it starts with a dollar, quote it :). Yes, you CAN learn the specific cases where you don't have to - but it's almost always harmless to quote them in cases where you don't have to, so you may as well just quote your variables everywhere! – Muzer Jun 3 at 10:15
  • 2
    @slebetman: And for the rest of us without this instinct, shellcheck will do the complaining ( shellcheck.net ). – Piskvor Jun 3 at 12:42
  • 3
    @Roel many decades ago, I learned -- the hard way -- that 99.99999% of the time it's your fault, not a bug in the compiler or interpreter... :) – RonJohn Jun 3 at 14:56

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