Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

In the "Command Search and Execution" section, the POSIX specification says that PATH is searched when finding a utility to execute (with some exceptions). Does the specification mention anywhere that PATH will be initialized to a value that guarantees that all standard utilities will be found?

Or do I need to do something like the following to guarantee that I'll never get a "command not found" error when trying to run a standard utility?

PATH=$(command -p sh -c 'printf %s "${PATH}"')

(see the POSIX specification of the command utility)

share|improve this question

1 Answer 1

up vote 11 down vote accepted

Yes and no. In a POSIX environment, the utilities must behave as described by the specification. In practice, this means that conforming versions of the utilities must be present in $PATH. However, when running your program on a POSIX-compliant system, you may be running it in a non-conforming environment. In practice, what often happens is that the OS has a legacy mode and a POSIX mode, and is in the legacy mode by default. Worse than missing some commands, the legacy mode tends to have incompatibilities in them, such as options with different meanings.

You can retrieve a good PATH with getconf. Of course, it's tricky, as getconf in the original $PATH may not be the right one. The Application Usage for command shows a way to do it:

command -p getconf PATH

As far as I understand the specification, this is not necessary if you are running your program in a POSIX-conforming environment; and if you aren't running your program in a POSIX-conforming environment, POSIX doesn't apply. However, this application usage can be taken as a recommendation: if this doesn't work, you can feel entitled to complain to your vendor that whatever they are doing regarding the letter of the POSIX spec, they are not complying with its spirit.

share|improve this answer
    
Heh, I didn't even notice that getconf PATH in the application usage section. Thanks! –  Richard Hansen May 24 '12 at 3:42

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.