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.
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityA 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
So, if you ask me:
Can it contain non-standard characters: It can.
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.