[Techtalk] Linux PATH question

Devdas Bhagat devdas at dvb.homelinux.org
Sat Oct 2 11:33:52 EST 2004


On 01/10/04 11:35 -0500, ed orphan wrote:
> I know how to view the Linux PATH; echo $PATH
> and how to add more to it using export,
> but how do you remove a path from PATH?

You have a few options:
1> Set the full PATH explicitly without the unwanted directory.
2> Run the PATH environment variable through sed/Perl.

A shell variable is set by assigning to it. The export directive makes
it available to its child processes.
So you set the PATH variable by simply assigning to it.

PATH=/bin:/usr/bin:/usr/local/bin:${HOME}/bin

To add to $PATH
PATH=$PATH:/usr/X11R6/bin

Alternatively,
PATH=/sbin:/usr/sbin:/usr/local/sbin:${PATH}

Make this available to child processes:
export $PATH

> Suppose you wanted to remover a directory surrounded by other
> directories, like /usr/bin in the PATH below:
>  /home/ourhome:/usr/bin:/lib:/usr/local/bin
This is what I would do:

[devdas at evita devdas]$ echo $PATH
/usr/kerberos/bin:/usr/lib/courier-imap/bin:/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/home/devdas/bin:/usr/X11R6/lib/xscreensaver:/usr/local/java1.4/bin

# First, check that we are removing the correct directory

[devdas at evita devdas]$ echo $PATH|sed -e 's~/usr/lib/courier-imap/bin:~~'
/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/home/devdas/bin:/usr/X11R6/lib/xscreensaver:/usr/local/java1.4/bin

# In this case, I am using ~ as the regexp pattern separator, instead of
# the traditional /.

# Set the PATH
[devdas at evita devdas]$ export PATH=`echo $PATH|sed -e 's~/usr/lib/courier-imap/bin:~~'`

# See, it has changed.

[devdas at evita devdas]$ echo $PATH
/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/home/devdas/bin:/usr/X11R6/lib/xscreensaver:/usr/local/java1.4/bin

Also:

[devdas at evita devdas]$ echo $PATH
/usr/kerberos/bin:/usr/lib/courier-imap/bin:/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/home/devdas/bin:/usr/X11R6/lib/xscreensaver:/usr/local/java1.4/bin

[devdas at evita devdas]$ PATH=`echo $PATH|sed -e 's~/usr/lib/courier-imap/bin:~~'`

[devdas at evita devdas]$ echo $PATH
/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/home/devdas/bin:/usr/X11R6/lib/xscreensaver:/usr/local/java1.4/bin

[devdas at evita devdas]$


> I used to be able to edit the PATH directly from .bash_profile
> but I can't even find where the PATH is stored in Red Hat.
> Yet another "improvement" I don't comprehend.

The default PATH is set from /etc/profile.

bash invokes /etc/profile, followed by ~/.bash_profile for a login
shell.

The lets you override global options set in /etc/profile within your
.bash_profile.

Devdas Bhagat


More information about the Techtalk mailing list