Is it possible to have special characters in alias?
A very dumb example, just to make the point:
ls | xargs cat | grep "this"
alias ->='xargs cat | grep '
ls | -> "this"
According to the bash man page, an alias must be a "name":
alias [-p] [name[=value] …]
... If arguments are supplied, an alias is defined for each name whose value is given.
and:
name
A word consisting solely of letters, numbers, and underscores, and beginning with a letter or underscore. Names are used as shell variable and function names. Also referred to as an identifier.
As of 5.2.15, at least, the man page states:
The characters /, $, `, and = and any of the shell metacharacters or quoting characters listed above may not appear in an alias name.
...
metacharacter
A character that, when unquoted, separates words. One of the following:
| & ; ( ) < > space tab newline
...
There are three quoting mechanisms: the escape character, single quotes, and double quotes
A non-quoted backslash (\) is the escape character.
In fact I didn't get to work the opening square bracket (it does work in dash), "[", the alias definition doesn't complain, but unable to "call" it later, I get:
unexpected EOF while looking for matching ``'
And escaping it with back slash doesn't match:
pm_7_aa[: command not found
However, if the first character of the alias name is itself a square bracket, it works...
It can be useful in this kind of syntax:
pm_7_aa{_ "$var" ; _}
[pm_7_aa: "$var" ; :]
pm_7_aa_[ "$var" ; ]_ # Only in dash as said
As a quirk for sending arguments to aliases, by using an open alias (to process the arguments calling a function) and a close alias (for example to set the positional parameters or define a local variable). Some kind of macros with aliases, I find it a very powerful syntax sugar.
For this question, it could then be:
alias -- -}='xargs cat | grep ' # For bash
alias -}='xargs cat | grep ' # For dash
ls | -} "this"
Yes it's perfectly possible to use special characters inside an alias or a function name.
Ex. :
alias ls∂="ls -al | grep '^d'"
ls∂() { ls -al | grep '^d' ; }
On the other hand it's clearly risky explosive business to use meta characters inside an alias or a function name.
-,>… ), and not special characters ( like√,≈,•… ), because you want to block their standard function to use them as plain characters. And my proposal is why not use a special character? For example:—( [alt]+[shift]+[-] ), orƒ