If I name a function or script with dash - instead of underline _ between words, would that be bad? For example, function duplicate-me() or duplicate-me.sh.

In bash, a variable name can contains underline but no dash. If I name functions and scripts using dash instead of underline, then bash will never intend to interpret such a name as a variable, and I can expect bash to give error message if I misuse such a name as a variable.


  • 1
    Variables and functions or scripts are different things. Why would you want bash to interpret one as the other? I often use dashes in script names as typing them doesn't need Shift to be pressed. – choroba Nov 17 '18 at 23:13

Using dashes is bad if you want to be portable, the standard allows only alphanumerics and underscores, so dashes might not work in all shells:

$ dash -c 'foo-bar() { echo foo-bar; }; foo-bar'
dash: 1: Syntax error: Bad function name

(Busybox and ksh also don't accept them; Bash, Zsh, and mksh do.)

That's not a problem if you know you're using Bash, so if you like dashes, you can use them in function names. However, if you want to avoid misusing a function in place of a variable name, note that e.g. $foo-bar and ${foo-bar} are both valid shell syntax. The first expands the variable $foo, and appends the string -bar; the second expands to the value of $foo, or if it's unset, the given value bar.


A POSIX shell function name can not contain dashes:

[A Name is] a word consisting solely of underscores, digits, and alphabetics from the portable character set. The first character of a name is not a digit.

zsh, bash, pdksh and fish allows dashes in function names, but ksh93 and dash do not (and fish is not really a POSIX shell to start with). I think it would be prudent to avoid dashes in function names.

Variables and functions don't share namespace, and the POSIX naming rules for variable names are the same as for POSIX shell function names, and I know of no shell that allows dashes in variable names.

Scripts can have dashes in their names and also any other character except for the nul-character and /, as they are filenames.


Yes, it's fine to use dashes in function names in bash.

Here's an example of a project using function names with dashes in them.

In fact, other characters you might find unusual are also possible in function names. Here's another example script in the same project, which uses ::s as part of function names, as some kind of namespacing. (Note, however, that the latter uses underscores, while the former uses dashes, so consistency is not that strong there...)

As mentioned by @choroba, the namespace for function names and variable names is separate, there's not really a situation where a name could refer to one or the other, so the fact that they have different sets of allowed characters doesn't really create any issues for names that are only valid as one and not the other.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.