Why does bash "echo [t]" result in "t" not "[t]"
bash
Solution
Being not a shell habitué (and not willing to become) I found surprising how filename expansion is designed to behave when no matches are found. I'll report the Bash reference
Bash scans each word for the characters `*`, `?`, and `[`. If one of these characters appears, then the word is regarded as a pattern, and replaced with an alphabetically sorted list of file names matching the pattern. If no matching file names are found:
- if the shell option `nullglob` is disabled, the word is left unchanged
- if the shell option `nullglob` is set the word is removed
- If the `failglob` shell option is set, an error message is printed and the command is not executed
The good news is this thing is configurable. The bad one is a script can fail in a number of ways one doesn't expect - at least, I did not, and it took me some time to understand why `echo` behaves the way you posted, just to find that it's because of a combination of weird filenames (who ever wants to name a file `t`?), hidden configuration (`nullglob` disabled, default option but still hidden) and a harmless command.
I said harmless because this is what you get, for example, when the target is `ls` (a failure because the file is not found):
raffaele@Aldebaran:~$ mkdir test
raffaele@Aldebaran:~$ cd test
raffaele@Aldebaran:~/test$ touch t
raffaele@Aldebaran:~/test$ ls [t]
t
raffaele@Aldebaran:~/test$ ls [v]
ls: cannot access [v]: No such file or directory
Problem
This happens for the character t, and the value root. Quite perplexing ``` $ echo [s] [s] $ echo [t] t $ echo [ t ] [ t ] $ echo [root] t ```