How to add a directory to $PATH in Linux 2022

This tutorial is about how to add a directory to $PATH on Linux. We will do our best for you to understand this guide. I hope you will like this blog How to Add Directory to $PATH in Linux. If your answer is yes, please share after reading this.
Check how to add directory to $PATH in Linux
In Linux or other Linux-like operating systems, when you type a command at the command prompt, you are simply telling it to run a program. Even simple commands like ls, mkdir, rm and others are just small programs, usually located in a directory on your computer called /usr/bin. There are other locations on your system that often contain executable programs as well; Some of the most common are /usr/local/bin, /usr/local/sbin, and /usr/sbin. What programs are where and why is beyond the scope of this article, but you should know that an executable program can exist virtually anywhere on your computer – it doesn’t need to be limited to one of these directories.
When you type a command in your Linux shell, it doesn’t look for a program with that name in every directory. It only searches for those you specify. How do you know to search the directories above? It’s simple: they’re part of an environment variable called $PATH that your shell checks to know where to look. Sometimes you may want to install programs in other locations on your computer, but you can still easily run them without specifying their exact location. You can easily do this by adding a directory to your $PATH.
What is $PATH in Linux?
- The $PATH environment variable is a colon-separated list of directories that tells the shell which directories to look for executable files.
- To check which directories are in your $PATH, you can use the printenv or echo command:
- The output will look like this
- /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
Add a directory to your $PATH
In some situations, you may want to add other directories to the $PATH variable. For example, some programs may be installed in different locations, or you may wish to have a dedicated directory for your personal scripts, but be able to run them without specifying the absolute path to the executable files. To do this, you just need to add the directory to your $PATH.
- Let’s say you have a directory called bin located in your home directory where you keep your shell scripts. To add the directory to your $PATH type:
- export PATH=”$HOME/bin:$PATH”
- The export command will export the modified variable to shell child process environments.
- You can now run your scripts by typing the name of the executable script without needing to specify the full file path.
- However, this change is only temporary and valid only in the current shell session.
- To make the change permanent, you must set the $PATH variable in your shell configuration files. On most Linux distributions, when you start a new session, environment variables are read from the following files:
- Shell-specific global configuration files such as /etc/environment and /etc/profile. Use this file if you want the new directory added to all system $PATH users.
- Per-user shell-specific configuration files. For example, if you’re using Bash, you can set the $PATH variable in the ~/.bashrc file. If you are using Zsh, the filename is ~/.zshrc.
- In this example, we will define the variable in the ~/.bashrc file. Open the file with your text editor and add the following line at the end:
- nano ~/.bashrc
- ~/.bashrc
- export PATH=”$HOME/bin:$PATH”
- Save the file and load the new $PATH into the current shell session using the source command:
- To confirm that the directory was added successfully, print the value of your $PATH by typing:
Final Words: How to Add a Directory to $PATH in Linux
Hope you understand this article How to Add Directory to $PATH in Linux, if your answer is no, you can ask anything via the contact forum section linked to this article. And if your answer is yes, share this article with your family and friends.