If I type * in the terminal, it is an alias for Desktop. But if I define a variable x and set it to *, it is not treated as such. When are certain characters treated as strings and when as operators ?

Asked
Active
Viewed 77 times
-1
Stéphane Chazelas
- 522,931
- 91
- 1,010
- 1,501
-
4Please don't post screenshots of text. Copy the text here and use code formatting instead: https://unix.stackexchange.com/editing-help#code – muru Jun 18 '23 at 12:48
-
4It has nothing to do with the _terminal_ and everything with the _shell_. You need to get familiar with the behavior of your shell (`zsh` for you, which behaves differently here than the more standard `bash`), around the keywords _patterns_, _wildcards_, _globbing_, _expansion_ etc. in order to understand what it does when it sees a `*`. Sorry but I can't give an exact explanation as I'm not familiar with `zsh`, I'm only familiar with `bash` which works differently here. – egmont Jun 18 '23 at 19:21
-
Related: [How to use a special character as a normal one in Unix shells?](https://unix.stackexchange.com/q/296141) – Stéphane Chazelas Jun 19 '23 at 16:12
1 Answers
4
Your shell expands * to the names of all files (except names that begin with ".") in the current directory via a mechanism called Filename Generation also known as globbing or Pathname Expansion.
In your case, "Desktop" was the first entry.
Look at echo *, or ls.
Stéphane Chazelas
- 522,931
- 91
- 1,010
- 1,501
waltinator
- 4,439
- 1
- 16
- 21
-
And for that glob expansion of happen upon parameter expansion, that is for the contents of `$x` to be considered a pattern (and it to be expanded to the matching file via filename generation when in list contexts), you need `$~x` or `${~x}` while a few other shells including bash do it always for any unquoted expansion which could be considered as a bug. – Stéphane Chazelas Jun 19 '23 at 16:09