Bash function name clash with system binary

bashshell-script

In my scripts I have a function called messages. I wrote it in Linux Mint, with no problems running it, and when I moved it to a Debian Buster station, the function clashes with /usr/bin/messages.

I have a startup script that calls the script messages:

startup_script

# call to messages script
. messages

messages

messages() {
  # reformat the arguments and return them
}

later on startup_script

messages "This is a message"

Which throws

./startup_script: line 35: .: /usr/bin/messages: cannot execute binary file
messages: could not open mailbox `/path/to/my/script/<string passed to my function>': No such file or directory

So I get a bunch of errors related to /usr/bin/messages being called instead of my function.

After adding type messages "This is a message", the relevant output is:

messages is /usr/bin/messages

I have the option of renaming my function¹, but maybe there's a better way to handle this situation.

How do I tell my script to ignore system binaries and use my own functions?


¹ The function is called in several scripts, many times, so it is not the easiest option to just change the name.

Best Answer

This is how . file works:

If file does not contain a <slash>, the shell shall use the search path specified by PATH to find the directory containing file.

This behavior is specified by POSIX.

Your first error is

./startup_script: line 35: .: /usr/bin/messages: cannot execute binary file

It's similar when you call . echo:

-bash: .: /bin/echo: cannot execute binary file

You're trying to source the binary file /usr/bin/messages. In effect your file where the function is defined is not sourced at all, the function is not defined in the current script. This means later messages still means /usr/bin/messages, not the function.

The relevant line should be . ./messages or . /full/path/to/messages (i.e. path to your file, not to the binary).

Related Question