How to add path to path linux

How to add path to path linux

How To Add A Directory To PATH In Linux

The other day I was testing a program named Macchina, which is written in Rust programming language. So I installed Rust using conda package manager in my Ubuntu system, and then installed Macchina using Rust’s cargo package manager.

When I tried to launch the Macchina program, the output said that the program is not installed. Again, I tried to install it and got the following error message:

As you might already know, when we install a program that is written in Rust, the executable binary files will be saved under Cargo’s bin directory (i.e.

/.profile. But in this case, I installed Rust inside a conda environment, and the cargo bin directory is not added to the PATH.

Sample output:

As you can see in the above output, the directory

In order to run a program using its name from any location, we need to add it to PATH as shown in the following section.

Add A Directory To PATH In Linux

Please mind the colon (:) at the end of the directory’s path.

Now list again the environment variables using echo command:

Sample output:

Please note that this is temporary. Once you exit from the current session, the environment variable will be gone.

To make the changes permanent, edit

Add the following line at the end:

Press CTRL+O followed by CTRL+X to save the file and exit.

Run the following command to take the changes into effect immediately:

Hope this helps.

How to add path to path linux. Смотреть фото How to add path to path linux. Смотреть картинку How to add path to path linux. Картинка про How to add path to path linux. Фото How to add path to path linux

Senthilkumar Palani (aka SK) is the Founder and Editor in chief of OSTechNix. He is a Linux/Unix enthusiast and FOSS supporter. He lives in Tamilnadu, India.

How to Add a Directory to PATH in Linux

Commands are not only disk operating system commands. The path is a list of directories in which the shell looks for operating system commands, and other commands. Commands are like short program files. So, commands are executables. Each application installed in the computer comes with an executable file. In order to run the application, the executable file is engaged first. These executable files are also called commands.

Without the PATH variable, each command would be commanded with the absolute path, as in,

/home/john/dir1/dir2/command.exe

where the first / is the root directory; john is the user directory for the user, John; dir1 and dir2 are sub-directories; and command.exe is the name of the executable file.

Many other names are possible for the executable file. In fact, command.exe is symbolic, here. So, if the directory, /home/john/dir1/dir2 (without the file), is in the PATH variable, the user john, will execute command.exe at the prompt, [email protected]:

$ by simply typing, command.exe, without the preceding path. That is:

and then pressing Enter.

In order to see the current list of PATH directories in the computer, type:

and press Enter, at the terminal. The result would be something like,

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

The question now is: how is a directory (and its preceding path), added to the PATH variable? With some installations, the directory is added automatically. With others, it has to be added manually. This article explains the two main ways, in which the directory is added. The directory is not added (included) as it would be done to an array. The two main ways are referred to as Temporary Addition and Permanent Addition. Bash is the shell used for the code examples in this article.

Article Content

Temporary Addition

Temporary addition means the addition only takes place in memory. It is not saved for use when the computer is rebooted.

The Bourne Shell Built-in Export Command

In simple terms, the export command is:

In this case, it reassigns a value to a variable in memory.

The PATH variable in memory may already have value like,

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

Assume that the directory to be added is /home/john/dir1/dir2. If the export command is typed as,

then /home/john/dir1/dir2 would replace all what is already in memory as value for the PATH variable.

If the export command is typed as,

Then, /home/john/dir1/dir2 would attach itself at the end of what is already in the variable. So, the new variable would be:

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/home/john/dir1/dir2

Now, the same command can be in more than one directory. When an executable command is typed without the preceding path at the prompt, the shell starts searching the value (string) of the PATH variable in memory, from left to right. So, with the above line of code, /home/john/dir1/dir2 would arrive at last, in the search. Who wants his directory to arrive last? – Nobody. So, a better way to include (add) the directory, is as follows:

/home/john/dir1/dir2 is now at the beginning, separated from the next directory, with a colon. So, with a prompt of “[email protected]:

after typing and pressing Enter, the command, command.exe in the directory, dir2, would be executed with:

As long as the computer is not shut down, the user will continue to execute command.exe without typing the preceding path.

When the computer is shut down, everything in random-access-memory is erased. The bad news is that the computer user will have to repeat this process of adding his directory to the PATH variable every time the computer is rebooted. Nobody wants to be doing that today. So, the Permanent Addition approach is the recommended approach. That is, the addition to PATH, has to be saved (into the hard disk).

Permanent Addition

From the above discussion, the idea is to save the change made (into the hard disk). So, if the change is saved into a file that shell reads each time the computer boots, that would be fine. That is, each time the computer boots, the PATH variable in memory will be updated appropriately. When a computer starts, at booting, it reads certain files. One of the files the Bash shell for Linux reads is,

Bash is just one shell, probably the most popular shell for Linux, today. Another shell for Linux is Zsh. With Zsh, the corresponding file is

/.zshrc, still in the user’s directory. When the shell starts, at booting, it reads this file. For Bash, the file is,

/.bashrc file, the directory will always be in memory, as

/.bashrc will always include it in PATH, each time the computer boots. It remains in memory, until the computer is shut down.

/.bashrc is a hidden file, so the ordinary use of the command, ls will not show it.

/.bashrc contains some Bash (shell) commands. Ubuntu is a variant of Linux. Ubuntu comes with a text editor. In the author’s computer, the background of the Ubuntu text editor is black.

The above addition command (without the prompt) should be included to the

/.bashrc file. It is good to add it at the end of the file in order not to temper with its available content, that is probably working alright.

/.bashrc in Ubuntu, type the following at the command prompt at the terminal, and press Enter:

Where nano is the text editor. Here, nano is a command (executable), whose argument is,

/.bashrc file will open in the text editor, probably with a black background. The cursor will be flashing at the top-left corner.

Press the down-arrow key on the keyboard continuously until the cursor reaches the end of the file. Add a new line of,

That is not all. The change to the

/.bashrc file has not been saved. To save the file with the change made, press Ctrl+o, and follow any other instructions that appear. To exit the text editor, press Ctrl+x, and the normal terminal window should replace the text editor window. Other shell commands can then be issued.

/.bashrc file into memory with the source command. It is simple to use the source command, as follows:

Note: Do not confuse between the end of the PATH value (list) and the end of the

/.bashrc file. Also, do not confuse between a directory in PATH in memory, and an executable file in the directory in the hard disk.

Conclusion

PATH is a built-in variable in Linux shells. The value of PATH is a list of directories separated by colons. There is a command (executable file) in the hard disk for each of these directories. If a command is issued at the sell without preceding it with a path, the shell will look into these directories for the command. If it does not see the command in any of the directories, then the command will not be executed. In this case, in order to execute the command, the command has to be preceded with its path. The command can be in more than one directory in the PATH value. As soon as the shell sees the first directory having the command, it executes the command. The PATH value is actually a string consisting of directories separated by colons.

The problem is how to add the directory for the executable of a new application into PATH. In some cases, this is done automatically by the installing program of the new application. In other cases, it has to be done manually. There are two ways of doing it manually, referred to as: Temporary Addition and Permanent Addition. Temporary Addition uses the export command and just adds the directory while the computer is on. Permanent Addition has to save the export command line into the

/.bashrc initialization file (Bash) for future operations.

How to correctly add a path to PATH?

How to add path to path linux. Смотреть фото How to add path to path linux. Смотреть картинку How to add path to path linux. Картинка про How to add path to path linux. Фото How to add path to path linux

How to add path to path linux. Смотреть фото How to add path to path linux. Смотреть картинку How to add path to path linux. Картинка про How to add path to path linux. Фото How to add path to path linux

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

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

/.bash_profile if that’s what you have.

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

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

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

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:

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; 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 add path to path linux. Смотреть фото How to add path to path linux. Смотреть картинку How to add path to path linux. Картинка про How to add path to path linux. Фото How to add path to path linux

Either way works, but they don’t do the same thing: the elements of PATH are checked left to right. In your first example, executables in

In particular, from a safety point of view, it is dangerous to add paths to the front, because if someone can gain write access to your

How to add path to path linux. Смотреть фото How to add path to path linux. Смотреть картинку How to add path to path linux. Картинка про How to add path to path linux. Фото How to add path to path linux

The bullet-proof way of Appending/Prepending

Try not using

Why? There are a lot of considerations involved in the choice of appending versus prepending. Many of them are covered in other answers, so I will not repeat them here.

/opt/bin ) to the PATH environment variable is

for appending (instead of PATH=»$PATH:

for prepending (instead of PATH=»

If parameter is null or unset, nothing is substituted, otherwise the expansion of word is substituted.

Note: This is for bash.

*1 I have just found that scripts like `devtoolset-6/enable` actually use this,

How to add path to path linux. Смотреть фото How to add path to path linux. Смотреть картинку How to add path to path linux. Картинка про How to add path to path linux. Фото How to add path to path linux

I’m confused by question 2 (since removed from the question since it was due to an unrelated issue):

What’s a workable way to append more paths on different lines? Initially I thought this could do the trick:

but it doesn’t because the second assignment doesn’t only append

This is a possible workaround:

but for readability I’d prefer to have one assignment for one path.

that’s all that will be in your PATH. PATH is just an environment variable, and if you want to add to the PATH, you have to rebuild the variable with exactly the contents you want. That is, what you give as an example to question 2 is exactly what you want to do, unless I’m totally missing the point of the question.

I use both forms in my code. I have a generic profile that I install on every machine I work on that looks like this, to accommodate for potentially-missing directories:

How to add path to path linux. Смотреть фото How to add path to path linux. Смотреть картинку How to add path to path linux. Картинка про How to add path to path linux. Фото How to add path to path linux

To add that directory to the end of the path, use the following command:

But the preceding are not sufficient because when you set an environment variable inside a script, that change is effective only within the script. There are only two ways around this limitation:

Inclusion basically incorporates the «called» script in the «calling» script. It’s like a #include in C. So it’s effective inside the «calling» script or program. But of course, it’s not effective in any programs or scripts called by the calling program. To make it effective all the way down the call chain, you must follow the setting of the environment variable with an export command.

How to add a directory to the PATH?

How to add path to path linux. Смотреть фото How to add path to path linux. Смотреть картинку How to add path to path linux. Картинка про How to add path to path linux. Фото How to add path to path linux

17 Answers 17

Using

/.profile, in that order, and reads and executes commands from the first one that exists and is readable.

How to add path to path linux. Смотреть фото How to add path to path linux. Смотреть картинку How to add path to path linux. Картинка про How to add path to path linux. Фото How to add path to path linux

How to add path to path linux. Смотреть фото How to add path to path linux. Смотреть картинку How to add path to path linux. Картинка про How to add path to path linux. Фото How to add path to path linux

The recommended place to define permanent, system-wide environment variables applying to all users is in:

(which is where the default PATH is defined)

This will work in desktop or console, gnome-terminal or TTY, rain or shine 😉

To edit, open the terminal and type:

(or open the file using sudo in your favorite text editor)

How to add path to path linux. Смотреть фото How to add path to path linux. Смотреть картинку How to add path to path linux. Картинка про How to add path to path linux. Фото How to add path to path linux

I think the canonical way in Ubuntu is:

create a new file under /etc/profile.d/

and give it execute permission

How to add path to path linux. Смотреть фото How to add path to path linux. Смотреть картинку How to add path to path linux. Картинка про How to add path to path linux. Фото How to add path to path linux

For complete newbies (like I am) who are more comfortable with GUI:

For persistent environment variables available to particular users only. I highly recommend Ubuntu official documentation.

Referring to documentation above, I have setup my Android SDK path-tools by:

How to add path to path linux. Смотреть фото How to add path to path linux. Смотреть картинку How to add path to path linux. Картинка про How to add path to path linux. Фото How to add path to path linux

Put that line in your

It gets sourced whenever you open a terminal

/.profile file. Probably shouldn’t do both however, as the path will be added twice to your PATH environment if you open a terminal.

How to add path to path linux. Смотреть фото How to add path to path linux. Смотреть картинку How to add path to path linux. Картинка про How to add path to path linux. Фото How to add path to path linux

/.bashrc also has the drawback that if you open/exec bash interactively from another interactive bash shell, it’ll be appended multiple times.

/.profile is correct for personal paths, though; that’s where Ubuntu adds the

/.profile in this case too.

According to this thread it appears as though Ubuntu’s behavior is slightly different than RedHat and clones.

To add the directory for only the logged-in user, append the same line to

In terminal, cd to the_directory_you_want_to_add_in_the_path

This wasn’t my idea. I found this way to export path at this blog here.

The recommended way to edit your PATH is from /etc/environment file

Example output of /etc/environment :

For example, to add the new path of /home/username/mydir

Then, reboot your PC.

System-wide environment variables

A suitable file for environment variable settings that affect the system as a whole (rather than just a particular user) is /etc/environment. An alternative is to create a file for the purpose in the /etc/profile.d directory.

/etc/environment

This file is specifically meant for system-wide environment variable settings. It is not a script file, but rather consists of assignment expressions, one per line.

Note: Variable expansion does not work in /etc/environment.

More info can be found here: EnvironmentVariables

How to add directory to system path in Linux

How to add path to path linux. Смотреть фото How to add path to path linux. Смотреть картинку How to add path to path linux. Картинка про How to add path to path linux. Фото How to add path to path linux

In Linux, the PATH environment variable stores the names of paths that will be searched for the executable files of any commands typed in the command line. The value of the PATH environment variable is a string containing several pathnames, each delimited by a colon. For instance, the default PATH on a typical system might look like this:

To view the current value of your PATH environment variable, you can use the echo command. As with all variables in the shell, when referring to the value you need to put a dollar sign before the variable name:

In the above example, the current value of path return you to the command prompt.

Setting PATH for your current shell session

You can set the value of PATH as you would any other shell variable, with the form NAME=VALUE, like this:

The problem with this command is that it will completely overwrite the values you had before, which you probably don’t want. If you want to add a new value in addition to the old ones. You can accomplish this by referring to PATH in the new definition, like this:

Using the command above adds your new path to the current value of PATH. Since the pathnames are searched in order, you probably want to add your new path at the end of the variable as we’ve done here. Instead, if you typed:

Your new path would be searched before, not after, the default system paths.

Using export to pass the PATH environment variable to child processes

This type of PATH definition sets the environment variable for your current shell session, but any new programs you run might not see the new path you’ve added. That’s because your shell lets you control the environment by requiring you to manually declare what environment variables are passed on to other programs and processes. You can accomplished this with the export command. If you run:

Any processes you run until you log out use the current value of PATH.

If you prefer, you can combine these two commands into a single line, for convenience. Put a semicolon between them so that the shell knows they’re separate commands:

If any of your pathnames have spaces in them, enclose the variable definition in quotation marks, to be safe.

Setting the PATH variable for every new shell session

The methods we’ve used so far only sets the environment variable for your current shell session; when you logout or close the terminal window, your changes will be forgotten. If you want to set PATH to a certain value every time you log in or start a new shell session, add it to your bash startup script. Every time you start an interactive shell session, bash reads the following files in order (if they exist), and executes the commands inside of them:

The first file, /etc/profile, is the default startup script for every user on the system. One or more of the remaining three files are located in the home directory of every user. Any of those three can be used, but it’s important to know that they will be searched for in this order.

You can edit these files and manually change any lines containing PATH= definitions. Be careful if you do so, because these are the directories used to locate important operating system files.

If you want to add a path for your current user only, you can leave the other PATH= lines untouched. Add a line like this to the end of the file:

If you add this to the end of the .bash_profile file in your home directory, it takes effect every time your user starts a new shell session. If you add this to /etc/profile, it takes effect for every user on the system. Note that you need administrator privileges if you want to edit /etc/profile, so you can use sudo (or be logged in as root) to do so.

Источники информации:

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *