1

I wanted to set gsettings as /usr/bin/gsettings so I created an alias. But I am not sure if that works:

$ type gsettings
gsettings is aliased to `/usr/bin/gsettings'
gsettings is /home/linuxbrew/.linuxbrew/bin/gsettings
gsettings is /usr/bin/gsettings

$ which gsettings /home/linuxbrew/.linuxbrew/bin/gsettings

Also another example:

$ type pandoc
pandoc is aliased to `/usr/bin/pandoc'
pandoc is /home/linuxbrew/.linuxbrew/bin/pandoc
pandoc is /usr/bin/pandoc
pandoc is /home/nikhil/.cabal/bin/pandoc

$ which pandoc /home/linuxbrew/.linuxbrew/bin/pandoc

Question

  1. Can someone please clarify which binary for pandoc and gsettings would get executed when I type pandoc and gsettings on bash?

  2. Does the order of output of type command has some significance?

Note

$ type type
type is a function
type () 
{ 
    builtin type -a "$@"
}
type is a shell builtin
terdon
  • 242,166
Porcupine
  • 1,892
  • It seems like you have the answer to the first part with which, do you have reason to believe that it's not the one returned by which? – user1794469 Jun 30 '20 at 11:35
  • @user1794469 which is basically a worse type, you don't need it and type is always better. which will only check for executables in your PATH, it will ignore the alias, so it is not relevant here. In fact, it is wrong, since it suggests that pandoc would run /home/linuxbrew/.linuxbrew/bin/pandoc, when instead it would run /usr/bin/pandoc because aliases take precedence. See Why not use "which"? What to use then?? – terdon Jun 30 '20 at 11:39

1 Answers1

2

Yes, the order is important: whichever one is first in the output of type is the one that will be executed. So, in your case, pandoc would run the alias, /usr/bin/pandoc, and gsettings would run /usr/bin/gsettings.

I can't actually find where this behavior is documented, where it is stated that the first result of type -a is the one that will be executed, but you can see it in action if you unset and then reset an alias, for example:

$ type -a ls
ls is aliased to `ls --color=tty'
ls is /sbin/ls
ls is /usr/bin/ls

$ unalias ls
$ type -a ls
ls is /sbin/ls
ls is /usr/bin/ls

$ alias ls='ls --color=tty'
$ type -a ls
ls is aliased to `ls --color=tty'
ls is /sbin/ls
ls is /usr/bin/ls

As you can see, the alias goes back to the beginning when it is re-added. Compare to:

$ touch ~/bin/ls; chmod 755 ~/bin/ls
$ type -a ls
ls is aliased to `ls --color=tty'
ls is /sbin/ls
ls is /home/terdon/bin/ls
ls is /usr/bin/ls

The new fake command I added, ~/bin/ls, is shown after the alias (aliases always take precedence), after /sbin/ls and before /usr/bin/ls. This is precisely the order of execution as you can see by checking the order of the directories in my $PATH:

$ echo "$PATH"
/sbin:/usr/sbin:/home/terdon/bin:/usr/local/bin:/usr/local/sbin:/usr/bin

Note how /home/terdon/bin is after /sbin and before /usr/bin, and how this order is reflected in the output of type.

Finally, the simplest way to know which one will be executed is to run type without -a:

$ type ls
ls is aliased to `ls --color=tty'

That always returns just one item and that is the one that will be executed when you use that command.

terdon
  • 242,166
  • Read the sequence of search in man bash under the COMMAND EXECUTION header (title in caps in the manual, I do not mean to yell here, if anyone thinks that I may, she/he would be mistaken). –  Jul 02 '20 at 12:51
  • @Isaac not sure what you mean. I read it, and it explains the sequence of command executions for the bash shell, yes. And we can perhaps assume that type will print its results according to the same sequence, is that what you mean? – terdon Jul 02 '20 at 13:09
  • Yes, that is exactly what I mean. –  Jul 02 '20 at 13:10
  • The man bash say for type: With no options, indicate how each name would be interpreted if used as a command name.. And the interpretation of command names is the command execution listed before. So yes, the manual states how type should work. –  Jul 02 '20 at 13:13
  • This is because you said: I can't actually find where this behavior is documented. But AFAICT it actually is. –  Jul 02 '20 at 13:14