Linux bashrc export 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.

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

Читайте также:  Линукс журнал i can

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 Export a Path in .Bashrc

When a command is launched on Linux, the operating system looks in the environment variable which is known as PATH for executable files. When we employ these paths, we can run the commands without having to provide a specific path.

What Are Environmental Variables in Linux?

In Linux, the environment variables are a collection of configurable named variables that were already saved on the device that is run on the terminal of Linux-based devices. The environment variable contains a name, and it has a specified value in it. We can alter the default path in Linux using the environment variable. For instance, the environment variable may keep track of the default search engine, the location of executable commands, or the keyboard configuration.

Here are the different techniques to view the default path. You can also add the path by yourself:

Technique 1: Default “Path” in Linux

If you want to display the default path in Linux, we write the following command in the Linux terminal:

The following is the default path of the Linux device. The file paths may vary somewhat based on your Linux system. As you verify in the following output, the colon “:” is used to separate each “Path” directory in the terminal, as well as some directories which are listed by default in the output. When a command is started, the system scans these directories from left to right.

If we write the “echo $Path” command and we didn’t get the path of the directory, we have to add the directory path. Here is an example: when we enter the command to view the path, we didn’t view any path as you can see in the following illustration:

Читайте также:  Huawei pc manager linux

Technique 2: Adding the Inode to a Temporary Path Variable

To create a temporary Path in Linux, we write the following command in the terminal:

linux@linux-VirtualBox:~$ export PATH = / usr / local / sbin: / usr / local / bin: / usr / sbin: / sbin: / bin:

/ usr / games: / usr / local / games: / snap / bin

Now, we can access the path by writing “echo” along with the path_name.

Here is the desired output of the command:

This method of creation only exists until we end the existing terminal which means that it is a temporary session. The PATH variable reverts to its default settings whenever you quit the existing activity and start another one, and the recently inserted inode won’t be present. An inode must be used to hold the PATH so that it is persistent throughout several terminal sessions. We use another method where it holds the inode permanently in the terminal session.

Technique 3: Permanently Adding the Inode at the End to a Path Variable

This method adds the Path permanently in the terminal configuration file. To add permanently, we use the “bashrc” command. Here is the complete statement:

After hitting enter, we get the following output:

To exit from the bashrc terminal, there are hints to exit the bashrc terminal at the bottom of the terminal. After coming back to the main terminal, write the “export PATH”. Then, assign the path of the inode of your choice in the Linux terminal. The following is the complete command to give the path of the inode:

After writing the previous command, wait so that the changes are made after entering the path of your choice in the Linux terminal. Then, save the bashrc file and close it.

Once a new terminal session is launched, the PATH variable is modified. Now, just employ the “source” statement to implement the modifications to the existing session. Use the following command to implement the source command to the bashrc file:

In the future, if you want to add a new inode in the bashrc file, use the colon “:” to add the directories to the existing “export” statement.

After importing the updated bashrc file, check whether the Linux existing address is saved in the Linux device default path or not. The following is the command to confirm the updated address/path. First, write “echo” and then write the “$PATH” to access the updated address.

Here is the desired output of the previously-implemented commands in the Linux terminal. As you see in the following output, we have now an updated address in the bashrc file because the new inode address is added to the previous path which is “/home/linux”.

Here is another command in Linux to display the current existing address in the bashrc file in the Linux terminal:

The following is the output of the “printenv” command which displays the same output because it works the same as “echo” in the Linux terminal:

Technique 4: Adding the Inode at the Start to a Path Variable in Linux Permanently

If you want to add a particular path at the start of the existing path in the bashrc file in Linux, you have to follow the instruction that is written in the following. This method is the same as we previously did. The only difference is to add the path before accessing the “$PATh” in the bashrc file in Linux.

Читайте также:  Команда дискового пространства линукс

After writing the previous command in the bashrc file, we now implement these changes in the file.Use the “source” command to update the bashrc file in Linux like this:

After updating the file, we now want to check whether the modifications are done or not in the bashrc file in the Linux terminal. Write the following written command:

The following is the output of the modification which we did in the bashrc file in the Linux terminal:

As you see in the output, the new path is added at the start of the previous path that is permanently saved in the bashrc file.

Conclusion

In this tutorial, we learned about one of the environment variables in Linux called “Path.” We learned how to get the default path in the terminal and what to do to add the path if it is not already there. With a thorough explanation, we learned how to permanently add the path at the beginning and at the end of the existing address in the bashrc file.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.

Источник

Where exactly in .bashrc set PATH?

I am using Debian Squeeze. In a similar question here it was advised to modify /etc/login.defs. I don’t want to do this as in the very login.defs it is written:

add the rest [of your paths] in the shell startup files 

3 Answers 3

You are using the wrong syntax. Drop the spaces:

export PATH=$PATH:/home/username/foo 

Regarding /etc/login.defs or any other global configuration: Well, it is global configuration, so probably a bad idea to add paths within your $HOME directory there. 😉

Just use the following line in your .bashrc

export PATH=/home/username/foo:$PATH 

If I don t remember bad.. It is a good practice put first $PATH. it avoids to overwrite native linux commands.

If the goal of adding custom executable binaries on the PATH is to override/wrap native functions/binaries, use the ‘export PATH=/home/username/foo:$PATH’ . If the goal is to append to the PATH only without overriding any native executable commands, use ‘export PATH=$PATH:/home/username/foo’. Alternatively, the user could namespace the custom binaries to avoid any overrides due to precedence order from configuration files, like .bashrc in this case.

There are differences in syntax between the one used on mac and CentOS, however on CentOS and RedHat the following syntax is being used.

export PATH=»/path/directory:$PATH» then do source .bashrc

I am not sure about other distributions of Linux but it will work on CentOS and RedHat.

Linked

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.14.43533

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

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