- UNIX / Linux: Set your PATH Variable Using set or export Command
- Finding out your current path
- How do I modify my path?
- Bash, Sh, Ksh shell syntax to modify $PATH
- Tcsh or csh shell syntax to modify $PATH
- Examples
- Learn How to Set Your $PATH Variables Permanently in Linux
- If You Appreciate What We Do Here On TecMint, You Should Consider:
- How to Permanently Set $PATH in Linux
- Understanding the $PATH Variable
- Using bash_profile to Set your PATH
- Using bashrc to Set your PATH
- Using a Profile File to Set your PATH
- Free eBook: Git Essentials
- Permanently Setting your PATH for Other Shells like ZSH and KSH
- Permanently Setting System-Wide PATH for all Users
- Troubleshooting PATH Problems
UNIX / Linux: Set your PATH Variable Using set or export Command
H ow do I add a new path to $PATH variable under Linux and UNIX like operating system? What is my path, and how do I set or modify it using csh/tcsh or bash/ksh/sh shell?
The PATH is an environment variable. It is a colon delimited list of directories that your shell searches through when you enter a command. All executables are kept in different directories on the Linux and Unix like operating systems.
Tutorial details | |
---|---|
Difficulty level | Easy |
Root privileges | No |
Requirements | None |
Est. reading time | 5m |
Finding out your current path
To find out what your current path setting, type the following command at shell prompt. Open the Terminal and then enter:
How do I modify my path?
To modify your path edit $PATH variable as per your shell. The syntax for setting path under UNIX / Linux dependent upon your login shell.
Bash, Sh, Ksh shell syntax to modify $PATH
If you are using bash, sh, or ksh, at the shell prompt, type:
Please feel free to replace /path/to/dir1 with the directory you want the shell to search.
Tcsh or csh shell syntax to modify $PATH
If you are using tcsh or csh, shell enter:
Please feel free to replace /path/to/dir1 with the directory you want the shell to search.
Examples
In this example add /usr/local/bin to your path under BASH/ksh/sh shell, enter:
To make these changes permanent, add the commands described above to the end of your
/.profile file for sh and ksh shell, or
/.bash_profile file for bash shell:
KSH/sh shell user try:
In this final example add /usr/local/bin/ and /scripts/admin/ to your path under csh / tcsh shell, enter:
To make these changes permanent, add the commands described above to the end of your
- No ads and tracking
- In-depth guides for developers and sysadmins at Opensourceflare✨
- Join my Patreon to support independent content creators and start reading latest guides:
- How to set up Redis sentinel cluster on Ubuntu or Debian Linux
- How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
- How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
- A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
- How to protect Linux against rogue USB devices using USBGuard
Join Patreon ➔
To verify new path settings, enter:
$ echo $PATH
See also
🐧 Get the latest tutorials on Linux, Open Source & DevOps via
Category | List of Unix and Linux commands |
---|---|
Documentation | help • mandb • man • pinfo |
Disk space analyzers | df • duf • ncdu • pydf |
File Management | cat • cp • less • mkdir • more • tree |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Linux Desktop Apps | Skype • Spotify • VLC 3 |
Modern utilities | bat • exa |
Network Utilities | NetHogs • dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • glances • gtop • jobs • killall • kill • pidof • pstree • pwdx • time • vtop |
Searching | ag • grep • whereis • which |
Shell builtins | compgen • echo • printf |
Text processing | cut • rev |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
Comments on this entry are closed.
I am a newbie on Linux.
I would like to ask if I can include an environment variable (e.g. ARCHIVES) that points to a directory (e.g. EXPORT ARCHIVES=/some/path/directory) to the .bash_profile, so that I dont do exporting all the time, everytime I need to use the directory?
How to set the CLASSPATH??
Or add as follows to your .bashrc file:
“echo ‘export PATH=$PATH:/usr/local/bin’ >>
Isn’t echo ” ‘PATH=$PATH:/usr/local/bin’ >>
/.bashrc ” a better idea?
how can I remove a path variable??
Hi,
To remove a path, go to”File System”. Open
/etc folder and edit (that is, remove )the path from the ‘environment’ text file. You can edit using the sudo command. Following are the commands.
cd
cd etc
sudo gedit environment
After removing the path from the “environment” file, save and restart the machine
Hi,
There was one mistake. It is “cd /”, not “cd
”
To remove a path, go to”File System”. Open
/etc folder and edit (that is, remove )the path from the ‘environment’ text file. You can edit using the sudo command. Following are the commands.
cd /
cd etc
sudo gedit environment
After removing the path from the “environment” file, save and restart the machine
To add a PATH for any user with sh or bash shell permanantly use the following steps.
1. Create a new file .profile in root(/) directory.
2. Add the following lines into it
PATH= path to enter
export PATH
3.save the file
4.exit and login to server again
5.check using echo $PATH
IT will work. Please let me know if tou have any queries on this .
The above one is only for root user
When I run my program I get this result:
terminate called after throwing an instance of ‘std::logic_error’
what(): basic_string::_S_construct NULL not valid
Aborted
Is this a result of having the wrong environment variable on my path or what. The program compiles without any errors. This is happening on Ubuntu (Linux, OS 10.0)
Could any one explain me about the functionality of command in shell script
set -xv
. /opt/app/etl/bin/profile.ksh
. `dirname $0`/env.cfg
Print input commands and their arguments as they are executed –> when you use set -xv
Hi there, thanks fo the article!
FYI, I just tried the syntax above for a tcsh but it didn’t work.
This works:
(Include this line directly in your .cshrc file. This example adds a dir called
/bin and your current dir to the previously existing PATH)
(Or, if you don’t want to open and edit your
/.cshrc file, type this in a teminal:)
Thanks for the heads up. The faq has been updated with correct syntax. FYI, the syntax setenv PATH $
Appreciate your post.
it helps me lots thanks………….
Hi :
I am new to linux.
May I ask how to convert this bash to tcsh?
Best Regards,
McGrady
why would this code be on my computer in a install file with along with macports pubkey and several other files.
SET doesn’t seem to do anything.
PATH as a variable name is case sensitive by me, but in this tut, this gets ignored.
Very bad. Didn’t helped me at all.
by mistake i changed defaults PATH ,how can i get default PATH from command line
i can’t even vi that hiden files
I have a machine that is running on kernel 2.6.32-504.16.2.el6.x86_64, I want to build a custom kernel using the same kernel on the same machine, but when I run make menuconfig, I get the following error
*** Unable to find the ncurses libraries or the
*** required header files.
*** ‘make menuconfig’ requires the ncurses libraries.
***
*** Install ncurses (ncurses-devel) and try again.
***
make[1]: *** [scripts/kconfig/dochecklxdialog] Error 1
make: *** [menuconfig] Error 2
I have the ncurses installed already in /lib64 and modified my PATH to point to /lib64, but I still get the same error, it looks like that it can not be found. does anyone have an idea why this is not working?
Install “ncurses-devel” package and try again.
Источник
Learn How to Set Your $PATH Variables Permanently in Linux
In Linux (also UNIX) $PATH is environment variable, used to tell the shell where to look for executable files. $PATH variable provides great flexibility and security to the Linux systems and it is definitely safe to say that it is one of the most important environment variables.
Programs/scripts that are located within the $PATH’s directory, can be executed directly in your shell, without specifying the full path to them. In this tutorial you are going to learn how to set $PATH variable globally and locally.
First, let’s see your current $PATH’s value. Open a terminal and issue the following command:
The result should be something like this:
The result shows a list of directories separated by colons. You can easily add more directories by editing your user’s shell profile file.
In different shells this can be:
/.bashrc or profile
Korn Shell ->
/.kshrc or .profile
Z shell ->
/.zshrc or .zprofile
Please note that depending on how you are logging to the system in question, different file might be read. Here is what the bash manual says, keep in mind that the files are similar for other shells:
Considering the above, you can add more directories to the $PATH variable by adding the following line to the corresponding file that you will be using:
Of course in the above example, you should change “/path/to/newdir” with the exact path that you wish to set. Once you have modified your .*rc or .*_profile file you will need to call it again using the “source” command.
For example in bash you can do this:
Below, you can see an example of mine $PATH environment on a local computer:
This is actually a good practice to create a local “bin” folder for users where they can place their executable files. Each user will have its separate folder to store his contents. This is also a good measure to keep your system secured.
If you have any questions or difficulties setting your $PATH environment variable, please do not hesitate to submit your questions in the comment section below.
If You Appreciate What We Do Here On TecMint, You Should Consider:
TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
We are thankful for your never ending support.
Источник
How to Permanently Set $PATH in Linux
Understanding the $PATH Variable
In this tutorial, we will show you how to permanently set your PATH on Linux.
First off, why should you care?
The $PATH variable, or just PATH, without the $ indicating variables, specifies a list of directories that impacts your computing platform’s functionality in a critical way. This is because the $PATH is the list of directories in which the system searches for executable programs, scripts, or files.
Imagine trying to run the ssh command, for example, to connect to a server. What happens if the system cannot find the ssh program? You are unable to connect to servers and run computations. You have a plethora of useful programs, examples like python , javac , npm , make , chmod , apt-get and so on, that your computer needs to be able to find when you invoke them at the command line.
The $PATH variable is the key that makes it possible to find the correct program and execute it at your command without needing the executable’s full directory path. When your PATH is set incorrectly, your shell will be unable to find programs, and certain commands will fail.
Using bash_profile to Set your PATH
A common mistake with the $PATH variable is to set it in the current shell only, without persisting the change. When you open a new shell, the changes are lost, and you are once again unable to execute certain commands because those programs are not found in the PATH.
The first way of setting your $PATH permanently is to modify the $PATH variable in your Bash profile file, located at /home/ /.bash_profile .
For example, let’s say I want to add a new directory /home/tomahawk/tools/jdk1.8.0_92/bin to my PATH. You might recognize this as a Java Development Kit installation. However, that is beside the point. Whatever the directory contains, I can add it to our path and make the programs that this bin directory contains accessible from the command line by adding the following line to the end of the file
A good way to edit the file is to use nano , vi , vim or emacs . You can use the command sudo
/.bash_profile , and enter your admin password when prompted, then add that line to the end and save the file.
To activate the changes in the current shell, you have to «source» the updated bash_profile file. You do this with the command:
This simply imports the file’s settings into the current shell. Now every time you open your shell, your bash_profile will automatically be «sourced» and you won’t need to run this command every time.
Now we can invoke commands or programs in the new directory /home/tomahawk/tools/jdk1.8.0_92/bin , such as javac , located at /home/tomahawk/tools/jdk1.8.0_92/bin/javac by just typing at the command prompt the name of the program. We can do this now from any directory, because the PATH has been updated to look for executable programs in our new directory.
Now runs the correct javac program, printing out something like the following:
bash_profile is appropriate when you want to set a PATH variable customized for a single user of the system. If you wanted to set PATH for all users of a system, there are better ways to do so, which we cover further on in this guide.
Using bashrc to Set your PATH
Instead of setting the PATH in
/.bash_profile , we can also add the directories we want to the PATH in
/.bashrc instead. Setting the PATH in bashrc looks identical to how we set it in bash_profile .
For example, to include the directory /home/tomahawk/.rbenv/bin in my path, I edit or create the file /home/tomahawk/.bashrc , adding the following line:
Notice that, like last time, the first thing in our new PATH export is the inclusion of the existing $PATH variable. This ensures that we preserve the current value of PATH, and just add any additional directories on to the PATH, after the $PATH variable. If you do not do this, you will overwrite the PATH variable entirely, and miss critical directories the system needs to be on the PATH. As a result, your system can become unusable.
The difference between using bashrc and bash_profile is that bash_profile is used for login shells. These run when you login via the console, or log in using ssh . In contrast, once you are logged in, and you open a command shell or run the bash command, the bashrc file will run. Your PATH settings from bashrc will then be available.
The effect of setting the PATH is similar. In addition, we must activate any changes in the bashrc file into the current shell the first time we make this change, just as we did for the bash_profile file. This time, we run source
/.bashrc . We can now access the new PATH at the command line. It has been set permanently and will stay the same between multiple logins into the system.
Using a Profile File to Set your PATH
We can also set the PATH permanently using a user’s profile file. This is different from
/.bash_profile in that it is set not for shells only, but for all programs.
User profiles are loaded at login. The PATH variable can be set in the
To set my PATH to include everything already in $PATH, as well as a new directory /home/tomahawk/.exenv/bin , I edit the file at
/.profile and set the PATH as follows
As in all prior examples, we will need to source these changes to make them active for the current shell, but subsequent logins will persist the changes.
Free eBook: Git Essentials
Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it!
Once that’s done, I can run the exenv command, which is one of the programs available in the folder I just added to the PATH, and I get back the output of my exenv version:
You can read more about the exenv program here, but you can use this process to set PATH permanently to include any program or directory you want.
Permanently Setting your PATH for Other Shells like ZSH and KSH
If you use alternative shells such as zsh, ksh and others, you can set the PATH permanently using those shells’ configuration.
Like bash, both zsh and ksh use a zshrc and khsrc file, respectively, to set the path for non-login shells. For login shells, they use the analogous shell profile files zprofile and kprofile.
You can therefore set the PATH permanently for these shells in a similar way to what we did for Bash. For zsh, you can find these files, or create them if they do not exist, at
Similarly, you can set PATH permanently for ksh in the configuration files located at
There are plenty of other shells you can use, such as the C Shell and the tcsh shell. Setting the PATH permanently for them will generally follow the pattern we have seen here.
Permanently Setting System-Wide PATH for all Users
System-wide settings for all users can be set in /etc/profile . There is considerable flexibility and multiple options for setting the PATH permanently system-wide.
Your Linux system will execute all script files ending in .sh in /etc/profile.d whenever a bash shell is entered, as well as when the desktop session loads.
You can therefore add a new file, such as env.sh inside the directory /etc/profile.d . In this file, we can export the PATH variable, setting it permanently to our choice of path directories, for example:
Files in /etc/profile.d are sourced by /etc/profile, thus activating our system-wide PATH whenever a user logs in.
We can also set PATH for all users in /etc/environment , which takes key-value pairs of the form:
Troubleshooting PATH Problems
As we saw, setting the PATH permanently in Linux has many options. You can set the PATH for only a certain user, for all users, or for only certain types of command shells. However, it’s a good idea to not fiddle with system-wide PATH settings unless you really know what you are doing.
If you encounter problems, a good starting point is to find out the current value of $PATH, by running the command:
For more troubleshooting tips, check out this resource.
Источник