How to Set or Modify the Path Variable in Linux

How to Set or Modify the Path Variable in Linux

Introduction

The Linux command line is a powerful tool that gives you complete control over your system. But to unleash its full potential, you must understand the environment in which it operates. One crucial component of this environment is the PATH variable. It’s like a guide that directs the system to where it can find the programs you’re asking it to run. In this article, we will delve into what the PATH variable is, why it’s important, and how to modify it to suit your needs.

What is the PATH Variable?

The PATH is an environment variable in Linux and other Unix-like operating systems. It contains a list of directories that the shell searches through when you enter a command. Each directory is separated by a colon (:). When you type in a command like ls or gcc, the system looks through these directories in the order they appear in the PATH variable to find the executable file for the command.

For example, if your PATH variable contains the following directories:

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin 

and you type ls, the system will first look for the ls executable in /usr/local/sbin. If it doesn’t find it there, it will move on to /usr/local/bin, and so on until it finds the executable or exhausts all directories in the PATH.

Why Modify the PATH Variable?

The default PATH variable usually works well for most users. However, there are scenarios where you might need to modify it:

  • Adding Custom Scripts: If you have custom scripts stored in a particular directory, adding that directory to your PATH allows you to run those scripts as commands from any location.

  • Software in Non-standard Locations: Some software may be installed in directories that are not in the default PATH. Adding such directories allows you to run the software without specifying its full path.

  • Productivity: Including frequently-used directories in your PATH can make your workflow more efficient, reducing the need to type full directory paths.

Temporarily Modifying the PATH Variable

Using the export Command

To temporarily add a new directory to your PATH for the current session, you can use the export command as follows:

export PATH=$PATH:/new/directory/path 

This modification will last until you close your terminal session.

Using the PATH=$PATH:/your/path Syntax

Alternatively, you can modify the PATH variable using the following syntax: