2

While doing some work, I came across this

$ echo [Login]
  g
$ echo [kilo]
  k
$ echo [gauke]
  e g k
$ echo [quasi-star]
  k m q

What is going on in the background that results in this output ? Output of echo [quasi-star] just lost it's mind giving letters that are not even in there.

Note: Yes I know I can get my output with echo "[Login]"

heemayl
  • 54,820
  • 8
  • 124
  • 141
GypsyCosmonaut
  • 3,988
  • 7
  • 38
  • 62

1 Answers1

6

Without the quotes, the shell is doing pathname (glob) expansion on the character class [], so presumably you have files named e, g, k, m, q in the current directory -- hence the result.

echo is basically just printing the result of pathname expansion, nothing more.


Let's take one example, echo [quasi-star]:

The result of expansion for [quasi-star] would be any single character from quasijklmnopqrstar (duplicates are kept intentionally). From shell's perspective, as there are no surrounding characters, so it would just match any single character filename on the current directory from the expansion.

heemayl
  • 54,820
  • 8
  • 124
  • 141
  • If I type `echo [kilo]` on my machine, the output ist `[kilo]` and in this directory is no file with an `k`, `i` or `l` in it's filename. – John Goofy Aug 07 '17 at 11:04
  • @JohnGoofy Yes, that's expected (literal value), unless you have `nullglob`/`failglob` (assuming `bash`) set. – heemayl Aug 07 '17 at 11:07
  • @heemayl But why there is only with `[kilo]` a literal echou output? – John Goofy Aug 07 '17 at 11:10