Add folder to path linux

How to correctly add a path to PATH?

I’m wondering where a new path has to be added to the PATH environment variable. I know this can be accomplished by editing .bashrc (for example), but it’s not clear how to do this. This way:

If there are already some paths added, e.g. PATH=$PATH:$HOME/.local/bin:$HOME/bin , another can be added by separating with a : e.g. PATH=$PATH:$HOME/.local/bin:$HOME/bin:/home/ec2-user/pear/bin .

12 Answers 12

The simple stuff

depending on whether you want to add ~/opt/bin at the end (to be searched after all other directories, in case there is a program by the same name in multiple directories) or at the beginning (to be searched before all other directories).

You can add multiple entries at the same time. PATH=$PATH:~/opt/bin:~/opt/node/bin or variations on the ordering work just fine. Don’t put export at the beginning of the line as it has additional complications (see below under “Notes on shells other than bash”).

If your PATH gets built by many different components, you might end up with duplicate entries. See How to add home directory path to be discovered by Unix which command? and Remove duplicate $PATH entries with awk command to avoid adding duplicates or remove them.

Some distributions automatically put ~/bin in your PATH if it exists, by the way.

Where to put it

Put the line to modify PATH in ~/.profile , or in ~/.bash_profile or if that’s what you have. (If your login shell is zsh and not bash, put it in ~/.zprofile instead.)

The profile file is read by login shells, so it will only take effect the next time you log in. (Some systems configure terminals to read a login shell; in that case you can start a new terminal window, but the setting will take effect only for programs started via a terminal, and how to set PATH for all programs depends on the system.)

Читайте также:  Starwind v2v converter linux

Note that ~/.bash_rc is not read by any program, and ~/.bashrc is the configuration file of interactive instances of bash. You should not define environment variables in ~/.bashrc . The right place to define environment variables such as PATH is ~/.profile (or ~/.bash_profile if you don’t care about shells other than bash). See What’s the difference between them and which one should I use?

Don’t put it in /etc/environment or ~/.pam_environment : these are not shell files, you can’t use substitutions like $PATH in there. In these files, you can only override a variable, not add to it.

Potential complications in some system scripts

You don’t need export if the variable is already in the environment: any change of the value of the variable is reflected in the environment.¹ PATH is pretty much always in the environment; all unix systems set it very early on (usually in the very first process, in fact).

At login time, you can rely on PATH being already in the environment, and already containing some system directories. If you’re writing a script that may be executed early while setting up some kind of virtual environment, you may need to ensure that PATH is non-empty and exported: if PATH is still unset, then something like PATH=$PATH:/some/directory would set PATH to :/some/directory , and the empty component at the beginning means the current directory (like .:/some/directory ).

if [ -z "$" ]; then export PATH=/usr/local/bin:/usr/bin:/bin; fi 

Notes on shells other than bash

In bash, ksh and zsh, export is special syntax, and both PATH=~/opt/bin:$PATH and export PATH=~/opt/bin:$PATH do the right thing even. In other Bourne/POSIX-style shells such as dash (which is /bin/sh on many systems), export is parsed as an ordinary command, which implies two differences:

  • ~ is only parsed at the beginning of a word, except in assignments (see How to add home directory path to be discovered by Unix which command? for details);
  • $PATH outside double quotes breaks if PATH contains whitespace or \[*? .

So in shells like dash, export PATH=~/opt/bin:$PATH sets PATH to the literal string ~/opt/bin/: followed by the value of PATH up to the first space. PATH=~/opt/bin:$PATH (a bare assignment) doesn’t require quotes and does the right thing. If you want to use export in a portable script, you need to write export PATH=»$HOME/opt/bin:$PATH» , or PATH=~/opt/bin:$PATH; export PATH (or PATH=$HOME/opt/bin:$PATH; export PATH for portability to even the Bourne shell that didn’t accept export var=value and didn’t do tilde expansion).

Читайте также:  Linux открыть порт 3306

¹ This wasn’t true in Bourne shells (as in the actual Bourne shell, not modern POSIX-style shells), but you’re highly unlikely to encounter such old shells these days.

Источник

Linux: Add a Directory to PATH

PATH is an environment variable that instructs a Linux system in which directories to search for executables. The PATH variable enables the user to run a command without specifying a path.

This article will explain how to add a directory to PATH temporarily or permanently as well as how to remove it in Linux.

Linux: add a directory to PATH

What Is Linux PATH?

When a user invokes a command in the terminal, the system executes a program. Therefore, Linux has to be able to locate the correct executable. PATH specifies program directories and instructs the system where to search for a program to run.

How to View the Directories in PATH

To print all the configured directories in the system’s PATH variable, run the echo command:

echo PATH terminal output

The output shows directories configured in PATH by default. The printenv command delivers the same output:

printenv PATH terminal output

Furthermore, running which on a certain command shows where its executable is. For instance, execute which with whoami :

which whoami terminal output

The output shows that the executable for whoami is located in the /usr/bin/ directory.

How Do I Add a Directory to PATH in Linux?

Specific directories are added to PATH by default. Users can add other directories to PATH either temporarily or permanently.

Linux: Add to PATH Temporarily

Temporarily adding a directory to PATH affects the current terminal session only. Once users close the terminal, the directory is removed.

To temporarily add a directory to PATH , use the export PATH command:

export PATH="/Directory1:$PATH"

export PATH terminal output

The command added Directory1 from the Home directory to PATH . Verify the result with:

echo PATH Directory1 added terminal output

The output shows that the directory was added to the variable. This configuration lasts during the current session only.

Linux: Add to PATH Permanently

Add a directory to PATH permanently by editing the .bashrc file located in the Home directory. Follow these steps:

1. Open the .bashrc file using a text editor. The example below uses Vim.

Opening .bashrc in Vim.

3. Paste the export syntax at the end of the file.

export PATH="/Directory1:$PATH"

Add directory to .bashrc file in Vim

5. Execute the script or reboot the system to make the changes live.

Читайте также:  Встроенный антивирус astra linux

6. To verify the changes, run echo :

echo PATH permanently added Directory1 terminal output

Editing the .bashrc file adds a directory for the current user only. To add the directory to the PATH for all users, edit the .profile file:

Add directory to profile file in Vim

Remove Directory from PATH in Linux

There is no single command to remove a directory from PATH . Still, several options enable the process.

Method 1: Exit the Terminal

Removing a directory from PATH is simple when it’s added temporarily. Adding the directory in the terminal works for the current session only. Once the current session ends, the directory is removed from PATH automatically.

To delete a temporary directory from PATH , exit the terminal or reboot the system.

Method 2: Edit Configuration Files

If the directory export string was added to the .bashrc or .profile file, remove it using the same method. Open the file in a text editor, navigate to the end of the file, and remove the directory.

Method 3: Apply the String Replacement Concept

To remove a directory from PATH , use string replacement:

String replacement terminal output

The command only removes the string from the current session.

Method 4: Use a One-Liner

Another option is to use the combination of tr, grep and paste to remove a directory from PATH . For instance:

export PATH="$( echo $PATH| tr : '\n' |grep -v Directory1 | paste -s -d: )"

tr grep -v paste terminal output

After reading this guide, you now know how to add a directory to the PATH variable. Next, learn how to export Bash variables in Linux.

Sara Zivanov is a technical writer at phoenixNAP who is passionate about making high-tech concepts accessible to everyone. Her experience as a content writer and her background in Engineering and Project Management allows her to streamline complex processes and make them user-friendly through her content.

The printf command produces formatted text outputs in the terminal. This tutorial shows how to use the printf command to print and format a variable output in Linux.

The cd command in Linux allows you to change the current working directory using the terminal window. Learn how to use the cd command in this tutorial.

Linux offers different ways to remove directories without using the GUI. In this tutorial, learn how to remove directories in the terminal window or command line using Linux commands.

Источник

Оцените статью
Adblock
detector