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?

2 Answers 2


POSIX and Hyphens: No Guarantee

According to the POSIX standard, a function name must be a valid name and a name can consist of:

3.231 Name
In the shell command language, a word consisting solely of underscores, digits, and alphabetics from the portable character set. The first character of a name is not a digit.

Additionally, an alias must be a valid alias name, which can consist of:

3.10 Alias Name
In the shell command language, a word consisting solely of underscores, digits, and alphabetics from the portable character set and any of the following characters: '!', '%', ',', '@'.

Implementations may allow other characters within alias names as an extension. (Emphasis mine.)

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:

$ a-b() { date; }
dash: 1: Syntax error: Bad function name

Interestingly enough, it does support hyphens in aliases, though, as noted above, this is an implementation characteristic, not a requirement:

$ a_b() { printf "hello %s\n" "$1"; }
$ alias a-b='a_b'
$ a-b world
hello world

The busybox shell (only the ash based one) also does not support hyphens in function names:

$ a-b() { date; }
-sh: Syntax error: Bad function name

Summary of Hyphen Support by Shell

The following shells are known to support hyphens in function names:

  • pdksh and derivatives, bash, zsh
  • some ash derivatives such as the sh of FreeBSD (since 2010) or NetBSD (since 2016.
  • busybox sh when the selected shell at compile time is hush instead of ash.
  • csh and tcsh (in their aliases, those shells have no function support). Those shells have a radically different syntax anyway, so there's no hope to have cross shell compatibility with those.
  • rc and derivatives (again with a radically different syntax)
  • fish (again with a radically different syntax)

The following shells are known not to support hyphens in function names:

  • the Bourne shell and derivatives such as ksh88 and bosh (in the Bourne shell, functions and variables shared the same namespace, you couldn't have a variable and a function by the same name).
  • ksh93, yash, the original ash and some of its derivatives (busybox ash (the default choice for sh), dash)


  • Hyphens are non-standard. Stay away from them if you want cross-shell compatibility.
  • Use underscores instead of hyphens: underscores are accepted everywhere.
  • 2
    Names with - in them are evil. I'm looking at you, CSS. :)
    – PM 2Ring
    Nov 16, 2014 at 11:16
  • 1
    Thanks. I tested later in csh and tcsh and they don't support hyphens either, but Korn shell did. It's slightly funny that I used a-b() as my function name as well.
    – iconoclast
    Nov 17, 2014 at 14:24
  • 3
    @PM2Ring they're easier to type than names with underscores. Excluding tradition or implementation decisions of systems that don't allow them, what principle can you point to to support the claim that they are evil?
    – iconoclast
    Feb 19, 2018 at 3:35
  • 2
    @iconoclast Names with hyphens are impossible in many languages, since the hyphen is interpreted as a minus sign, so if such a language needs to interoperate with a language that supports names with hyphens you get the messy situation where entities with hyphenated names are forced to have a different name in the other language.
    – PM 2Ring
    Feb 21, 2018 at 12:29
  • 3
    @PM2Ring yes, that's a good point, and I agree that that's a pain, but should one language be restricted to being consistent with the decisions made by a previous language's designer?
    – iconoclast
    Feb 21, 2018 at 19:25

I know this is really late, but perhaps you can work around your issue of making the underscore more accessible.

xmodmap -e "keycode  20 =  underscore minus"

This will switch underscore with hyphen (minus).

So now, you hold shift for hyphen, but an underscore is typed without shift.

Your keycode may be different, however, I think it depends on your keyboard; mine happens to be 20. Just let me know if you need help finding what keycode you need to use.

  • 1
    Simple & brilliant. I need _ about a thousand times for each time I need -. My programs may have a couple of expressions with - in them, while there are _ everywhere. Now it just remains to be seen how long it takes to get used to this new mapping...
    – Roger Dahl
    Jan 30, 2021 at 23:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .