1

I want to know which executable gets executed for any command in bash.

Example:

  1. I have firefox installed here /usr/bin/firefox, it is in the $PATH
  2. alias browser=firefox
  3. alias br=browser

Now I want to type something like getexecutable "br" and it should display /usr/bin/firefox

bricklore
  • 177
  • I think you're looking for something like type. – saiarcot895 Jul 23 '15 at 21:34
  • type does't recurse through aliases :( – bricklore Jul 23 '15 at 21:49
  • That's not really a thing, though, as you can have multiple commands in an alias, so it doesn't really make sense to say you can recurse through them. You can have something like alias a='echo b; cat c | tac'. Type will do as much as possible; any more, you can write a script to parse the output of type and run type recursively until it's what you want, but prepare for it to break in cases like the one I mentioned above. – Muzer Jul 23 '15 at 22:21
  • You can hit Ctrl-Meta-e to expand aliases on the command line one level each at the time. – jimmij Jul 23 '15 at 22:24

1 Answers1

1

Here's a quick script I wrote further to my comment, that in the SIMPLE case of aliases will work. For anything with arguments/etc., though, it will fail miserably.

cmd="$1"
type=aliased
while [ "$type" = "aliased" ]; do
    output="$(type "$cmd")"
    type="$(cut -d ' ' -f 3 <<< "$output")"
    cmd="$(cut -d '`' -f 2 <<< "$output" | tr -d \')"
done
echo "$output"

You will have to (ironically!) alias something to source this, as spawning a subshell will likely remove your local aliases.

Muzer
  • 2,293
  • Imagine alias a=b and alias b=a now a is also an executable in $PATH -- bash will detect the alias loop and execute /usr/bin/a correctly. type will be stuck in a serious loop :D – bricklore Jul 23 '15 at 22:37
  • Indeed; as I said, my script will only work in the simple case, as you seemed only to be considering the simple case there. The complex cases make your question stop making any sense, as I mentioned in my comment to your question! – Muzer Jul 23 '15 at 22:38
  • Yeah - I need this to prevent any firefox or other applications from being executed. And this without modifing /usr/bin in any way... I just dont want them. – bricklore Jul 23 '15 at 22:40
  • Can't you just alias firefox to false or something? (Assuming we're talking about times when aliases would be honoured, ie in places in the shell). If you want to stop firefox from being executed in the more general case without modifying /usr/bin at all, I'm not sure that's actually possible. All you can do is plug up some of the more obvious ways it could be executed. – Muzer Jul 23 '15 at 22:51
  • Yeah I thought getting the executable and preventing the execution would be more roboust. But anyway I think you're right, it might not be possible the way i would like it to be – bricklore Jul 23 '15 at 22:56
  • Why would you source this? Stick it in a function, that's exactly what they're for. – Gilles 'SO- stop being evil' Jul 23 '15 at 23:42
  • I always feel putting a whole function in my .bashrc or whatever to be a bit messy, but maybe that's just me. – Muzer Jul 24 '15 at 07:39