Linux add home directory to path

How to permanently set $PATH on Linux/Unix [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.

This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.

Background

This works, however each time I exit the terminal and start a new terminal instance, this path is lost, and I need to run the export command again. How can I do it so this will be set permanently?

If you are on a mac, then bashrc works fine, no need to continuously source ~/.profile for the terminal to read from the environment variables

24 Answers 24

You need to add it to your ~/.profile or ~/.bashrc file.

Depending on what you’re doing, you also may want to symlink to binaries:

cd /usr/bin sudo ln -s /path/to/binary binary-name 

Note that this will not automatically update your path for the remainder of the session. To do this, you should run:

source ~/.profile or source ~/.bashrc 

A couple of questions. 1) Shouldn’t there be a colon between $PATH and /usr/bin . 2) Should /usr/bin even be there. 3) Shouldn’t you rather use /usr/local/bin ?

Please note: it’s often considered a security hole to leave a trailing colon at the end of your bash PATH because it makes it so that bash looks in the current directory if it can’t find the executable it’s looking for. Users who find this post looking for more information should be advised of this.

@AdamRobertson It is unsafe- consider the scenario when you unpack a tarball, then cd to the directory you unpacked it in, then run ls —and then realize that the tarball had a malicious program called ls in it.

I think I significantly improved the quality of this answer, and addressed a few issues which other users brought up. Every path export, or every command which adjusts the path, should always make sure to separate an existing path with a colon. Leading or trailing colons should never be used, and the current directory should never be in the path.

Читайте также:  Linux how to cut string

There are multiple ways to do it. The actual solution depends on the purpose.

The variable values are usually stored in either a list of assignments or a shell script that is run at the start of the system or user session. In case of the shell script you must use a specific shell syntax and export or set commands.

System wide

  1. /etc/environment List of unique assignments. Allows references. Perfect for adding system-wide directories like /usr/local/something/bin to PATH variable or defining JAVA_HOME . Used by PAM and systemd.
  2. /etc/environment.d/*.conf List of unique assignments. Allows references. Perfect for adding system-wide directories like /usr/local/something/bin to PATH variable or defining JAVA_HOME . The configuration can be split into multiple files, usually one per each tool (Java, Go, and Node.js). Used by systemd that by design do not pass those values to user login shells.
  3. /etc/xprofile Shell script executed while starting X Window System session. This is run for every user that logs into X Window System. It is a good choice for PATH entries that are valid for every user like /usr/local/something/bin . The file is included by other script so use POSIX shell syntax not the syntax of your user shell.
  4. /etc/profile and /etc/profile.d/* Shell script. This is a good choice for shell-only systems. Those files are read only by shells in login mode.
  5. /etc/.rc . Shell script. This is a poor choice because it is single shell specific. Used in non-login mode.

User session

  1. ~/.pam_environment . List of unique assignments, no references allowed. Loaded by PAM at the start of every user session irrelevant if it is an X Window System session or shell. You cannot reference other variables including HOME or PATH so it has limited use. Used by PAM.
  2. ~/.xprofile Shell script. This is executed when the user logs into X Window System system. The variables defined here are visible to every X application. Perfect choice for extending PATH with values such as ~/bin or ~/go/bin or defining user specific GOPATH or NPM_HOME . The file is included by other script so use POSIX shell syntax not the syntax of your user shell. Your graphical text editor or IDE started by shortcut will see those values.
  3. ~/.profile , ~/._profile , ~/._login Shell script. It will be visible only for programs started from terminal or terminal emulator. It is a good choice for shell-only systems. Used by shells in login mode.
  4. ~/.rc . Shell script. This is a poor choice because it is single shell specific. Used by shells in non-login mode.
Читайте также:  Can linux be customized

Notes

GNOME on Wayland starts a user login shell to get the environment. It effectively uses the login shell configurations ~/.profile , ~/._profile , ~/._login files.

Man pages

Источник

How to Add a Directory to PATH in Linux

Learn all the essential steps about adding a directory to the PATH in Linux and making those changes permanently.

The PATH variable in Linux stores the path to the directories where it should look for executables when you run a command.

[email protected]:~$ echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

As you can see, the PATH consists of several directories (like /usr/local/sbin, /usr/bin and more) separated by a colon (:). If you want to run some executables as commands from anywhere in the system, you should add their location in the PATH variable. This is common while setting up a development environment. For example, imagine you downloaded and installed Java and Maven. To make your programs work properly, you’ll need to specify the location of the binaries of Maven and Java in the PATH. This quick tutorial is about setting up PATH in Linux. Apart from the steps, I’ll also mention things you should be careful about while dealing with PATH.

Adding a directory to PATH in Linux

export PATH=$PATH:your_directory

Where your_directory is the absolute path to the concerned directory. Let’s say you download and extract Maven to the home directory and you want to add its bin directory to the PATH. Let’s assume that the absolute path of this bin directory is /home/abhishek/maven/apache-maven-3.8.0/bin. Here’s what you should be doing: export PATH=$PATH:/home/abhishek/maven/apache-maven-3.8.0/bin

export PATH=$PATH:/home/abhishek/maven/apache-maven-3.8.0/bin
  • The $ before a variable name means you are referring to its value. PATH is the variable name, $PATH is the value of the variable PATH.
  • You should not use $ with PATH on the left side of =
  • There must not be any spaces before and after =
  • Don’t forget to include the : after $PATH because the directories in the PATH are separated by a colon.
  • There must not be a space before and after the colon (:).

Once you have set the PATH with the new value, please check that the PATH has been correctly updated.

[email protected]:~$ echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/home/abhishek/maven/apache-maven-3.8.0/bin

You may want to run the command or script for which you modified the PATH. This will tell you for sure if the PATH is correctly set now.

Читайте также:  Сборка linux web сервер

Making the changes to PATH permanent

You added the desired directory to the PATH variable, but the change is temporary. If you exit the terminal, exit the session or log out from the system, the PATH will revert, and the changes will be lost.

If you want to make the changes to the PATH variable permanent for yourself, you can add it to the .bashrc file in your home directory, assuming you are using the Bash shell.

You can use a text editor like Nano or Vim for this task.

add directory to path linux

If you want the modified PATH variable to be available for everyone on the Linux system, you can add the export to the /etc/profile file. This is suitable when you are a sysadmin and have a configured system with a custom path.

💡See the path in a simplified view

Reading the PATH variable values and figuring out the directories could be complicated for some people. You can use the tr command to format its output to make it more easily readable.

Bonus tip: The directories take precedence in PATH

There are several directories in the PATH variable. When you run an executable file/command, your system looks into the directories in the same order as they are mentioned in the PATH variable.

If /usr/local/sbin comes before /usr/bin, the executable is searched first in /usr/local/sbin. If the executable is found, the search ends and the executable is executed.

This is why you’ll find some examples where the additional directory is added before everything else in PATH:

export PATH=your_directory:$PATH

If you think that your additional directory should be searched before everything else you should add it before the $PATH; otherwise, add it after $PATH.

Was it clear enough?

I highly suggest reading up on the concept of absolute and relative paths in Linux. It will give you a better understanding of the topic.

I have tried to explain things with the necessary details but not going too deep into details. Does it make the topic clear, or are you more confused than before? If you still have doubts, please let me know in the comments.

Источник

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