wisew wisew - 8 months ago 40
Bash Question

What happens if two command line programs share the same name?

I've always wondered this, and haven't managed to find a good answer anywhere – what happens if two command line programs aren't namespaced properly and share the same name? How would you call one or the other? Say, for instance, there were two programs that responded to

brew install
– which would be called by default? How would you specify the other?

Answer Source

There are four things that can be invoked when you type a command on the command line. They are used in this order:

  1. alias
  2. function
  3. builtin
  4. file(s)

You can use type -a [program] to see which are available and the order in which they are found. When you have an alias, you can skip it and run the next one found with a \ (i.e. \echo). You can call the built-in command with builtin (builtin echo), and the first file found with command (command echo). You can specify a particular file to execute by providing a path (with at least one /) to it. When you do not specify a path to a file, your shell will look at your PATH, a colon-separated list of directories in which to look. It will check for a file with your command name in each of the folders specified in your PATH, and it will execute the first one it finds.