1

Am writing bash functions. It looks like function names can include hyphen: -.

Thus indus-headrc () and indus-tailrc (). Had thought that only underscore is allowed.

0

1 Answer 1

0

A common misconception about Bash is that function names must follow the same rules that variables do. The Bash manual even suggests this:

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.

In fact, Bash function names can be almost any printable character. For instance, I can define my own pre-increment unary function:

function ++ { (( $1++ )); }

Of course, any sensible person would still adhere to the recommendation of standard function naming.


Try writing POSIX-ly (#!/bin/sh), it would not accept hyphens with erroring out:

Syntax error: Bad function name


Conclusion

So, if you ask me:

  1. Can it contain non-standard characters: It can.

  2. Should I use it then. Definitely no! You will harm portability, thus you should not use any non-standard naming. Adhere to POSIX, it's a great way to grow good habits.

0

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