Bash – Should we use UTF-8 characters like ⏰ in bash/shell script

bashshellunicode

The simple code here is working as expected on my machine if launched with bash :

function ⏰(){
 date
}
⏰

Could there be a problem for other people using this, or is it universal ?

I'm wondering because I've never seen anything like this in other source code for now.

Edit : There are unlimited possibilities, it can be used to quickly distinguish a function role with the usage of an emoji for example.

A ? for something that can modify or remove files, a ? if it's a work in progress, ? for an interactive menu…

I guess we should create a standard for all of that, but it seems to be an interesting idea.
Maybe a random line of ~5 characters can help us a lot understanding what the code is doing. (Of course we need to learn how to read them.)

More edit : I'm giving it a shot. For now, if i fold all my functions in my editor (Or cat myscript.sh|grep function) they look like this. (My unicode looks much better in geany or my terminal compared to here.)

function ⬚_1(){
function ⬚⬚_2(){
function ⬚⬚⬚_?_D(){
function ⬚⬚⬚⬚_?_X(){
function ⬚⬚⬚⬚⬚_?_Y(){
function ⬚⬚⬚⬚⬚⬚_❓_P(){
function ⬚⬚⬚⬚_?_Z(){
function ⬚⬚⬚⬚⬚_❓_U(){
function ⬚⬚⬚⬚⬚_❓_O(){

I use a strange indentation ⬚ to show how the functions are related to each other and a symbol ?/❓ to clearly distinguish their role. (Of course these are not my real function names, I just put a random letter at the end, but even without them we can clearly see the relationships.)

Best Answer

A useful guideline for this is the "Portable Operating System Interface" (POSIX), a family of standards that is implemented by most Unix-like systems. It is usually a good idea to limit shell scripts to features mandated by POSIX to make sure they will be usable across different shells and platforms.

According to the POSIX specification of function definitions in the "Shell Command Language":

The function is named fname; the application shall ensure that it is a name (see the Base Definitions volume of IEEE Std 1003.1-2001, Section 3.230, Name). An implementation may allow other characters in a function name as an extension.

Following the link to the definition of a "name":

In the shell command language, a word consisting solely of underscores, digits, and alphabetics from the portable character set.

That character set contains only characters between U0000 and U007E.
Therefore characters like "⏰" (U23F0) are not valid in a POSIX-compliant identifier.

Your shell might accept them, but that doesn't guarantee that others will as well.
To be able to use your script across different platforms and software versions, you should avoid using non-compliant identifiers like this.

Related Question