Linux root set path

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.)

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.

Читайте также:  Ошибка монтирования astra linux

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).

¹ 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.

Источник

How to Permanently Set $PATH in Linux

Set PATH in Linux 1

In Linux, $PATH is a system-dependent environment variable. This variable tells the shell where to find executable commands.

Setting the PATH variable in a Linux system provides greater flexibility and security by allowing users to set the path in various effective ways permanently.

The $PATH that you specify impacts the Linux system in a critical way. The shell searches for the path to that command in the $PATH variable when you execute any command. For instance, when you run an SSH command, it searches for the actual location of SSH in the $PATH environment variable.

This article discusses several ways to utilize the $PATH environment variable to provide flexibility to the Linux system.

Why is My $PATH Execution Giving an Error?

If you enter an external command in shell, for example, myprogram.c , it gives an error as shown below:

Читайте также:  What steam games can you play on linux

Set PATH not found

It means that the shell is unable to find the command in the $PATH variable. You can check the $PATH variable by printing it using the echo command:

You will see the output similar to the one given below:

Set PATH in Linux

The output lists all the directories added to the environment variable. All the directories are separated by a colon.

It is crucial to add a path to this environment variable when you execute any program. To check the path of a program, type and execute:

You will see the following output:

And you can see this path already added in the $PATH variable.

set path in PATH variable

In other words, if you set the $PATH variable accurately, you can use various commands on Linux like the TCC compiler or Java JDK. However, if you do not set the path appropriately, the execution of that particular command will fail.

How to Resolve $PATH Error

Let’s look at one more example. Create a folder named scripts using mkdir command. Then, create a shell script file called test1.sh .

Now, if you try to run this file, it will give the same error.

$PATH not found

Let’s try setting this path to the $PATH environment variable. The syntax is:

PATH=$PATH:/new/directory/here or PATH=/new/directory/here:$PATH

The first syntax will add the path at the end of the $PATH variable. Similarly, the second syntax will add the path at the beginning of the $PATH variable.

Now, to set your path, type:

Hit enter and try executing the file again. It will run successfully. If you check the $PATH variable, you will see that this new path exists in the $PATH environment variable.

However, this will set the path temporarily. To permanently add the path to the $PATH variable, there are multiple ways. Please note that profile files vary according to the shell type. In bash shell, it can be ~/.bash_profile , ~/.bashrc , or .profile . In Korn shell, it is ~/.kshrc or .profile . Similarly, in Z shell, it is ~/.zshrc or .zprofile .

Set PATH Variable Using .bash_profile

The first mistake that users often make is to set the path temporarily. It results in losing all the currently added paths in the environment variable. The first method to add the path permanently to an element variable is to store it in .bash_profile . It will customize the path for the single user of the system.

To permanently set the PATH variable in Linux to .bash_profile , edit the file using vi , nano , vim , or emacs command:

export PATH=$PATH:/path/to/your/directory

The command adds the current directory to the element variable and preserves the previous existing paths. If you do not add the $PATH element with the export command, you will lose all the previously existing paths, and the system might malfunction due to the missing variables.

Читайте также:  Linux cache buffer size

To reload the changes, type the source command:

You can also confirm that the path exists in the environment variable by echoing the $PATH :

If you are using Z shell or Korn shell, you can perform the steps in the profile files. Instead of .bash_profile , the file would be ~/.zshrc and ~/.zprofile for Z shell. Similarly, it will be ~/.kshrc and ~/.kprofile for Korn shell. You can also use shells like C Shell or tcsh shell.

Set PATH Variable Using .bashrc

Instead of adding paths in .bash_profile , you can also set paths to the directories in .bashrc . Setting the path environment variable in bashrc is similar to setting it in .bash_profile . To add the path in bashrc , type:

export PATH="$PATH:/home/user/.rbenv/bin"

The location to bashrc file is: /home/user/.bashrc

The difference between bash_profile and bashrc is that bashrc is specifically used for login via the console or login using SSH. Moreover, source the changes as you did with bash_profile by executing the command:

User-Wide Initialization

Another method is to set the user profile instead of the .bash_profile . The changes will be for current users only. Depending on your shell type, you can add the files in .profile , or .zprofile .

The user profile file is located at /home/user directory , or ~/.profile .

Open .profile file and type

# ~/.profile export PATH=$PATH:/path/to/your/directory

Change “/path/to/your/directory with the exact path that you want to set. Then activate the new $PATH by using the source command.

System-Wide Initialization

System-wide settings for all users are available in the /etc/profile . To add a path to the $PATH environment variable in a way that affects all the users, add the path in the /etc/profile/ . It will be visible to all the users of the system.

However, you must have root privileges to execute this step. To add the path, edit .profile in the etc directory:

# /etc/profile export PATH=$PATH:/path/to/your/directory

This guide explains various methods by which you can add a path to your environment variable. You can set the PATH variable in Linux temporarily or permanently. You also have the flexibility to set the path user-wide or system-wide, and for different types of command shells.

In case, you encounter an error, better check $PATH first; you can check this using the command:

However, users must not change path variables unless they know what they are doing. To learn even more about path variables, check the Wikipedia page.

Sidrah is a staff writer at Distroid and has a Masters in Computer Science, with her key two focus areas being SQL and DevOps. She has written over 100 posts and specializes in all things Linux. She also has experience trying out various other distros, BASH scripting, Git, and software testing. LinkedIn GitHub

Leave a Reply

You must be logged in to post a comment.

Источник

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