Adding any current directory './' to the search path in Linux

LinuxPathDirectoryAdd

Linux Problem Overview


How do you add any current directory './' to the search path for executables in Linux?

Linux Solutions


Solution 1 - Linux

I know this is an old answer, but if anyone else stumbles across this question via Google like I did, here's a more detailed explanation.

If you want to make it so that search path contains the value of pwd at the time you set the search path, do:

export PATH=$PATH:$(pwd)

So, if pwd is /home/me/tmp, PATH will be set to $PATH:/home/me/tmp

However, If you want it so that whatever your present working directory is at the time you execute a command (ex; the value of pwd at any given time is in the search path), do:

export PATH=$PATH:.

So, if pwd is /home/me/tmp, PATH will be set to $PATH:.. If your present working directory contains a script called foo, then it would be fount in your PATH. If you change directories to one that does not contain foo, "foo" will not be found in the PATH any more.

You should note that having your present working directory in your PATH is a potential security risk, however.

Solution 2 - Linux

If you want to permanently add the directory you're currently in to the PATH variable you can use

$ echo "export PATH=\$PATH:$(pwd)" >> ~/.bashrc

which will expand $(pwd) to the string literal of your current directory and append the quoted line to your bashrc. Note the \ in \$PATH is needed to escape the expansion of $PATH to its current value.

$ pwd
/path/to/suuuuuuuuuuuuuuuuuuuuper/long/foo/directory/bin

$ echo "export PATH=\$PATH:$(pwd)" >> ~/.bashrc

$ tail ~/.bashrc -n 1
export PATH=$PATH:/path/to/suuuuuuuuuuuuuuuuuuuuper/long/foo/directory/bin

Solution 3 - Linux

For the current directory, you can just use a zero-length (null) directory name. You can use an initial or trailing colon, or a double colon. This is from the bash manpage, man bash:

PATH   The  search path for commands.  It is a colon-separated list of
       directories in which the shell looks for commands (see COMMAND EXECUTION
       below).  A zero-length (null) directory name in the value of PATH
       indicates the current directory.  A null directory name may appear as two
       adjacent colons, or as an initial or trailing colon.	The default path
       is system-dependent, and is set by the administrator who installs bash.
       A common value is
       ``/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin''.

Solution 4 - Linux

Um...that didn't work for me. I would do

export PATH=$(pwd):$PATH

The command previously posted literally just adds the dot.

Solution 5 - Linux

export PATH=$PATH:$PWD 

works with bash 4.3.48

Solution 6 - Linux

This is an old question, but I thought I'd add to it for those using the CSH or TCSH.

Adding the following to your .cshrc or .tcshrc will add the current directory to the environment path variable.

setenv PATH {$PATH}:.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBrian MckinlayView Question on Stackoverflow
Solution 1 - LinuxJustin DoyleView Answer on Stackoverflow
Solution 2 - LinuxMatthew WooView Answer on Stackoverflow
Solution 3 - LinuxdosentmatterView Answer on Stackoverflow
Solution 4 - LinuxoliverView Answer on Stackoverflow
Solution 5 - LinuxgerardwView Answer on Stackoverflow
Solution 6 - LinuxMBWView Answer on Stackoverflow