How To Export Path In Linux

In Linux, exporting a path is a common task that allows us to specify directories to the system where it should look for executables when running commands. By adding directories to the $PATH variable, you can run scripts and programs located in other directories without needing to specify the full path.

Understanding the $PATH Variable

The $PATH variable is a global variable in Linux that the system uses to locate required executables. This variable consists of colon-separated directories. The system searches these directories when it needs to find a file for executing a command.

Exporting a PATH in Linux

To export a PATH in Linux, you simply need to use the export command followed by the variable you wish to set.

 
    export PATH=$PATH:/path/to/your/directory 

In this example, replace /path/to/your/directory with the full path to the directory you would like to add to your $PATH variable. The $PATH at the beginning ensures that you are adding to the existing PATH instead of overwriting it.

Permanently Exporting a PATH in Linux

The method above will only export your PATH for the duration of the session. If you want to permanently add a directory to your PATH, you will need to add the export command to the .bashrc file in your home directory.

    echo 'export PATH=$PATH:/path/to/your/directory' >> ~/.bashrc

Again, replace /path/to/your/directory with the directory you want to add. This line uses the echo command to append the export command to your .bashrc file.

Then, to ensure the changes take effect immediately, you need to run:

    source ~/.bashrc

Now, the system will always look for executables in the directory you have added, even after you log out and log back in.

Conclusion

Adding a directory to your PATH in Linux makes it easier to run scripts and programs without needing to specify their full paths. Using the export command, you can add directories to your PATH either temporarily or permanently.