1

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"
1
  • Your question is about meta-characters ( i.e. characters which have a function like -, >… ), 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 ƒ Commented Jan 29 at 18:57

3 Answers 3

1

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.

1

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"
0

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.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.