Alias one program in a different order than $PATH

1

I alias pandoc='~/.cabal/bin/pandoc' because I don't want to change my $PATH order just for this one program.

In general, programs I installed in /opt/, ~/.cabal/, and so on should precede defaults in /bin/, but I don't want bash to search through thirteen special directories just to find /bin/cat, which seems like it should be fast and immediate.

Is there a more appropriate middle-ground than simply aliasing over particular programs whenever they seem to be executing the wrong version?

isomorphismes

Posted 2015-02-23T20:02:26.553

Reputation: 1 534

Answers

2

Bash maintains a table where it caches the full path to executables - see, e.g., http://bradconte.com/bash-path-hashing. The first time you invoke /bin/cat in a Bash session, the full path to it will be stored in this table. The second time you run /bin/cat, Bash doesn't actually search the search path again. In a sense, that's fast and immediate.

You can even use the hash (Bash) built-in in the way it's explained in this nice answer, in order to cache the full path to the pandoc executable:

hash -p ~/.cabal/bin/pandoc pandoc

From help hash:

hash: hash [-lr] [-p pathname] [-dt] [name ...]
    Remember or display program locations.

    Determine and remember the full pathname of each command NAME.  If
    no arguments are given, information about remembered commands is displayed.

    Options:
      -d                forget the remembered location of each NAME
      -l                display in a format that may be reused as input
      -p pathname       use PATHNAME is the full pathname of NAME
      -r                forget all remembered locations
      -t                print the remembered location of each NAME, preceding
                each location with the corresponding NAME if multiple
                NAMEs are given
    Arguments:
      NAME              Each NAME is searched for in $PATH and added to the list
                of remembered commands.

    Exit Status:
    Returns success unless NAME is not found or an invalid option is given.

Edward

Posted 2015-02-23T20:02:26.553

Reputation: 774

oh, awesome! Nice to know; thanks for taking the time to answer. – isomorphismes – 2015-02-24T16:24:47.950