In my testing (in Bash and Z Shell), I saw no problems with defining functions or aliases or executable shell scripts which have hyphens in the name, but I'm not confident that this will be okay in all shells and in all use cases.
The reason I would like to do this is that a hyphen is easier to type than an underscore, and therefore faster and smoother.
One reason I'm hesitant to trust that it's not a problem is that in some languages (Ruby for example) the hyphen would be interpreted as a minus sign even without spaces around it. It wouldn't surprise me if something like this might happen in some shells, where the hyphen is interpreted as signaling an option even without a space.
Another reason I'm a little suspicious is that my text editor screws up the syntax highlighting for functions with hyphens. (But of course it's entirely possible that that's just a bug in its syntax highlighting configuration for shell scripts.)
Is there any reason to avoid hyphens?
Best Answer
POSIX and Hyphens: No Guarantee
According to the POSIX standard, a function name must be a valid name and a name can consist of:
Additionally, an alias must be a valid alias name, which can consist of:
A hyphen is not listed among the characters that must be allowed in either case. So, if they are used, portability is not guaranteed.
Examples of Shells That Do Not Support Hyphens
dash
is the default shell (/bin/sh
) on the debian-ubuntu family and it does not support hyphens in function names:Interestingly enough, it does support hyphens in aliases, though, as noted above, this is an implementation characteristic, not a requirement:
The busybox shell (Almquist shell) also does not support hyphens in function names:
Summary of Hyphen Support by Shell
The following shells are known to support hyphens in function names:
The following shells are known not to support hyphens in function names:
Conclusions