Search code examples
shposix

Using absolute path to command


I'm writing a command which uses the environment variable CC. If CC is set, my command makes a system call to $CC, otherwise cc is used. When invoking cc, should I use /usr/bin/cc or simply cc? Can I even rely on cc existing in /usr/bin? Since a user can set CC to any string I think using the path to cc provides no extra safety.


Solution

  • cc is not part of the POSIX standard, so you cannot assume any particular location for it. (Really, the same is true for any command; that is what PATH is for.) Your script has one documented requirement, which is that it be able to run some command to compile code.

    1. By default, that command is cc.
    2. You can modify PATH to influence which cc gets used.
    3. You can set CC to be more specific; it can be another command name, a relative path, or an absolute path.

    Note that CC and PATH are not mutually exclusive. If CC is set to an absolute path, that will be used. If you just use something like CC=gcc, then the value of PATH can still determine which of several installed versions of gcc are used.