Linux add bin 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.

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.
Читайте также:  Linux cores and threads

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.

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

Читайте также:  Настройка ssh ключей linux

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

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 raspberry pi computer

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

Источник

Add a binary to my path

I have an executable. I want to execute the executable in terminal with name only like other commands. I can put my executable in /usr/local/bin or I could add its PATH to ~/.bashrc . Both will work. What is better? Is there any difference?

4 Answers 4

For example let me assume, you have an executable myscript . You need to run it from a terminal as,

User level Change

If you add the PATH of that executable to ~/.bashrc , you can run the executable with name only from anywhere (Avinash Raj already mentioned), as

But the change will be affected in user level. That means if you have any other user(s) they could not access the executable with name only. If they have proper permission they need to run the executable as,

Also, you will not be able to run the script as sudo as it is not in PATH of root, To run as sudo you need to use,

system level change

If you put your script in /usr/local/bin it can be accessed system wide and for all users. In that case any user can run your executable as (subject to having proper permissions)

In that case you can run the executable as sudo also as,

Now choose one way depending upon your need.

Adding the location of the file to your $PATH variable in your ~/.bashrc file will only allow you to execute from any location, whereas putting it in /usr/bin/ will allow all users on your system to execute that file from any location.

Why is that? Because, your ~/.bashrc file is only visible to you as a user. So all variable changes done are limited to you. Whereas, adding that file to /usr/bin will allow the file to remain there for all users and since /usr/bin is present in the $PATH variable unless someone removes it, will allow all users to execute it from any location.

If you put the executable file in /usr/bin or /usr/local/bin , then you will be able to run that program by only specifying the name ( your-program instead of /usr/local/bin/your-program ).

If you instead add the directory containing the program to the ~/.bashrc file to the PATH , then any executable file present in that directory can be launched by only typing its name.

This information is false, you can stil launch the executable even if you are not in «that directory».

Ok, I had a hard time reading your answer (and thereby made an error) so I edited it to remove the confusion and remoted the downvote. Hopefully it helps.

So, the question is: What is better? Is there any difference? You practically didn’t say nothing more than the OP.

Источник

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