Which characters are allowed in a bash alias
bash, linux, macos
Solution
Bash aliases definitely can't contain these characters: space ` `, tab `\t`, newline `\n`, `/`, `\`, `$`,
The Bash Reference Manual says:
The characters `/`, `$`, , `=` and any of the shell metacharacters or quoting characters listed above may not appear in an alias name.
"Shell metacharacters" are defined as
A character that, when unquoted, separates words. A metacharacter is a `space`, `tab`, `newline`, or one of the following characters: `|`, `&`, `;`, `(`, `)`, `<`, or `>`.
"Quoting characters" aren't explicitly listed but they are the backslash `\` and the single `'` and double `"` quotes.
Bash checks alias names in `legal_alias_name()` which is implemented here: 1, 2, 3. Looking through the output of mksyntax.c (a C program that generates another C program) on my system, `shellbreak()` will return true for these 10 characters: `\t\n &();<>|`, `shellxquote()` for these 4: `"'\``, and `shellexp()` for `$`, finally `legal_alias_name()` also checks for `/`.
However, there are other limitations from 1) how Bash tokenizes the `alias` expression that you would have to write to define the alias and 2) how it will tokenize a command that starts with your alias. Namely, for 1), `=` can't appear in an alias name, probably because `alias foo=bar=baz` would be parsed as `foo` = `bar=baz`, not `foo=bar` = `baz`. For 2), from my experiments, you can define an alias with `!`, the history expansion character, but if you try to use it, you'll get a `command not found` error, unless the `!` is at the end of the command. There's probably other characters, the safest bet is to stick to numbers, letters and the underscore.
Problem
I recently added ``` alias ..='cd ..' alias ...='cd ../..' alias ....='cd ../../..' ``` to my bash_aliases file. Playing around with this, I noticed that while aliasing `..` was allowed, aliasing `.` was not. What other punctuation characters are allowed in bash aliases?