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.

4
  • 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
    Commented Jun 3, 2019 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. Commented Jun 3, 2019 at 4:26
  • @G-Man I did find DannyNiu's answer helpful in terms of the information provided, "just in case". Commented Jun 3, 2019 at 4:28
  • Try the set -f (and set +f) options before/after the final echo to disable globbing/wildcard expansion if this behavior is unwanted.
    – MattBianco
    Commented Mar 12 at 9:52

1 Answer 1

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.

6
  • 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. Commented Jun 3, 2019 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
    Commented Jun 3, 2019 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
    Commented Jun 3, 2019 at 10:15
  • 2
    @slebetman: And for the rest of us without this instinct, shellcheck will do the complaining ( shellcheck.net ). Commented Jun 3, 2019 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
    Commented Jun 3, 2019 at 14:56

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