Setting a POSIX $PATH

Problem

You are on a machine that provides older or proprietary tools (e.g., Solaris) and you need to set your PATH so that you get POSIX-compliant tools.

Solution

Use the getconf utility:

PATH=$(PATH=/bin:/usr/bin getconf PATH)

Here are some default and POSIX paths on several systems:

# Red Hat Enterprise Linux (RHEL) 4.3
$ echo $PATH
/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/home/$USER/bin

$ getconf PATH
/bin:/usr/bin


# Debian Sarge
$ echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/usr/games

$ getconf PATH
/bin:/usr/bin


# Solaris 10
$ echo $PATH
/usr/bin:

$ getconf PATH
/usr/xpg4/bin:/usr/ccs/bin:/usr/bin:/opt/SUNWspro/bin

# OpenBSD 3.7
$ echo $PATH
/home/$USER/bin:/bin:/sbin:/usr/bin:/usr/sbin:/usr/X11R6/bin:/usr/local/bin:/usr/
local/sbin:/usr/games

$ getconf PATH
/usr/bin:/bin:/usr/sbin:/sbin:/usr/X11R6/bin:/usr/local/bin

Discussion

getconf reports various system configuration variables, so you can use it to set a default path. However, unless getconf itself is a built-in, you will need a minimal path to find it, hence the PATH=/bin:/usr/bin part of the solution.

In theory, the variable you use should be CS_PATH. In practice, PATH worked every-where we tested while CS_PATH failed on the BSDs.

Get bash Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.