How to Add a Directory to $PATH in Linux

How to Add a Directory to $PATH in Linux

This guide is about How to Add a Directory to $PATH in Linux. So read this free guide, How to Add a Directory to $PATH in Linux. If you have query related to same article you may contact us.

How to Add a Directory to $PATH in Linux – Guide

On Linux or other Linux-like operating systems, when you type a command at the command prompt, you are just 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 also contain executable programs; Some common ones 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 pretty much anywhere on your computer: it doesn’t have to be limited to one of these directories.

When you type a command in the Linux shell, it doesn’t look for a program by that name in every directory. It only looks for the ones you specify. How does he know how to look in 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 to other locations on your computer, but be able to easily run them without specifying their exact location. You can easily do this by adding a directory to your $PATH.

What is $PATH on Linux

  • The $PATH environment variable is a colon-delimited 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 be something like this
  • /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
  • Adding a directory to your $PATH

    There are situations where you might want to add other directories to the $PATH variable. For example, some programs might be installed in different locations, or you might want 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, just 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 command will export the modified variable to the shell’s child process environments.
  • Now you can run your scripts by typing the name of the executable script without having to specify the full path to the file.
  • However, this change is only temporary and valid only in the current shell session.
  • To make the change permanent, you need to set the $PATH variable in the shell’s configuration files. On most Linux distributions, when you start a new session, the environment variables are read from the following files:
  • Global shell-specific configuration files such as /etc/environment and /etc/profile. Use this file if you want the new directory to be added to all system users $PATH.
  • Per-user shell-specific configuration files. For example, if you are using Bash, you can set the $PATH variable in the ~/.bashrc file. If you are using Zsh, the file name will be ~/.zshrc.
  • In this example, we will set the variable in the ~/.bashrc file. Open the file with your text editor and add the following line in final from him:
  • 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 successfully added, print your $PATH value by typing:
  • Final note

    I hope you like the guide How to Add a Directory to $PATH in Linux. In case if you have any query regards this article you may ask us. Also, please share your love by sharing this article with your friends.

    We will be happy to hear your thoughts

    Leave a reply

    How to Guides, Best Lists, Top 10 and Tech News - Compsmag