Bash – Upstart and Bash Function Names Containing Hyphens

bashshellupstart

I inherited some bash scripts where the author used "-" in the function names instead of the more normal "_" e.g.:

function a-b {
...
}

Ugly but legal in bash. Problem is I sourced this inside an upstart script and got

x.sh: line 5: `a-b': not a valid identifier

Took a while to realize it was the name and not the function contents. Anyway, renaming is not an easy option in this case and as I've learned, upstart uses /bin/sh not /bin/bash to parse? run its scripts. Hence I guess this issue.

Any ideas on a workaround other than renaming?

If nothing else, at least I hope this question will warn others on the perils of odd bash coding styles and how they come back to bite you sometimes.

Best Answer

From the Upstart cookbook, Changing the Default Shell. There are 3 options, the first 2 involve changing your default shell from /bin/sh to something else. But the 3rd option looks like it would solve your particular issue.

excerpt

Use a "here document" (assuming your chosen shell supports them) within the Job Configuration Files you wish to run with a different shell:

script
/bin/bash <<EOT

echo "Hi - I am running under the bash shell"

date

echo "and so am I :)"

EOT
end script