How to permanently set $PATH on Linux/Unix?

2018-12-31 04:26发布

I'm trying to add a directory to my path so it will always be in my Linux path. I've tried:

export PATH=$PATH:/path/to/dir

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?

21条回答
十年一品温如言
2楼-- · 2018-12-31 04:53

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.

System wide

  1. /etc/environment List of unique assignments. Perfect for adding system-wide directories like /usr/local/something/bin to PATH variable or defining JAVA_HOME.
  2. /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.
  3. /etc/profile and /etc/profile.d/* Shell script. This is a good choice for shell-only systems. Those files are read only by shells.
  4. /etc/<shell>.<shell>rc. Shell script. This is a poor choice because it is single shell specific.

User session

  1. ~/.pam_environment. List of unique assignments. 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 variable including HOME or PATH so it has limited use.
  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 Shell script. It will be visible only for programs started from terminal or terminal emulator. It is a good choice for shell-only systems.
  4. ~/.<shell>rc. Shell script. This is a poor choice because it is single shell specific.

Distribution specific documentation

查看更多
若你有天会懂
3楼-- · 2018-12-31 04:53

I think the most elegant way is:

1.add this in ~./bashrc file

if [ -d "new-path" ]; then
  PATH=$PATH:new-path
fi

2.source ~/.bashrc

(Ubuntu)

查看更多
浪荡孟婆
4楼-- · 2018-12-31 04:54

You can also set permanently, editing one of these files:

/etc/profile (for all users)

~/.bash_profile (for current user)

~/.bash_login (for current user)

~/.profile (for current user)

You can also use /etc/environment to set a permanent PATH environment variable, but it does not support variable expansion.

Extracted from: http://www.sysadmit.com/2016/06/linux-anadir-ruta-al-path.html

查看更多
荒废的爱情
5楼-- · 2018-12-31 04:56

You may set $PATH permanently in 2 ways.

  1. To set path for particular user : You may need to make the entry in .bash_profile in home directory in the user.

    e.g in my case I will set java path in tomcat user profile

    [tomcat]$ echo "export PATH=$PATH:/path/to/dir" >> /home/tomcat/.bash_profile
    
  2. To set common path for ALL system users, you may need to set path like this :

    [root~]# echo "export PATH=$PATH:/path/to/dir" >> /etc/profile
    
查看更多
心情的温度
6楼-- · 2018-12-31 04:56

Add to /etc/profile.d folder script [name_of_script].sh with line: export PATH=$PATH:/dir. Every script within /etc/profile.d folder is automaticaly executed by /etc/profile on login.

查看更多
何处买醉
7楼-- · 2018-12-31 04:58

Zues77 has the right idea. The OP didn't say "how can i hack my way through this". OP wanted to know how to permanently append to $PATH:

sudo nano /etc/profile

This is where it is set for everything and is the best place to change it for all things needing $PATH

查看更多
登录 后发表回答