How to set the $ PATH variable in Linux

0


The $ PATH variable is a critical component on Linux and other Unix-like operating systems. It gives a list of directories that contain various executable files on the system and tells the shell where to look for those executable files.

On a fresh Linux installation, the directories contained in $ PATH ensure that most of the programs you are running will function properly. However, there are times when you want to quickly run custom scripts or programs on your system.

In such cases, you must set the $ PATH variable appropriately before running these programs.

What is $ PATH on Linux?

As mentioned above, $ PATH is an environment variable in Linux that comprises a colon-separated list of directories that the Linux shell refers to when it receives a command or exec call from a program.

Usually the $ PATH variable contains the /Container, / usr / bin, and the / usr / local / bin Directories, whereby the superuser receives two more entries, namely / sbin and / usr / sbin. However, you can add new entries to your $ PATH variable in order to conveniently start scripts and programs from the terminal regardless of the current working directory.

Why should you set $ PATH?

By and large, most of the programs you will ever run on your Linux system will work fine, and your shell will not display any errors at the time of execution. For example, if you run the ls command, the shell will run it without errors, without you having to provide the full path to the command.

However, if you need to run a custom script, you cannot declare the exec command as you would with the command ls Command ahead. Instead, you’ll either have to navigate to the script’s directory (via Terminal) and run the command from there, or, if you want to run the command from anywhere on the system, provide the absolute path of the script in the command to get the shell to help to find it.

Related: How to Change Directory in Linux Using cd Command

While both methods work well, they are not very efficient and can quickly turn the entire process into a tedious routine. An alternative (read-efficient and effective) way to run programs from non-standard directories is to add their source directory to your shell’s $ PATH so that it knows where to find the script and can therefore run it quickly.

How to set the $ PATH variable in Linux

Depending on what you want to accomplish – whether it’s a temporary setup to run a script only once, or whether you want to use the script regularly – there are several ways you can add a directory to your $ PATH.

1. Temporary setting of the $ PATH variable

If you only want to run a script or program in your current (active) session, you can set a temporary $ PATH variable. If you do, you can run this program with one command from anywhere on your system without having to include its full path in the command, only in the current session.

To temporarily set a $ PATH variable, open Terminal and use the following syntax to add directories to the variable:

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

For example:

export PATH=$PATH:/home/smithy/scripts/

Please note that with this configuration you can only run the program in your active session. If you temporarily set your $ PATH variable, the system will reset it to the default after you restart your computer.

2. Set a permanent $ PATH variable

If you have a program on your system that you are likely to use frequently, you will need to set the $ PATH variable for it permanently. This ensures that your shell remembers your directory even after a restart.

In contrast to the temporary solution, however, setting $ PATH permanently requires a few additional steps: Identify the shell used, edit the corresponding configuration file and set the $ PATH variable there.

To identify the shell on your system, run the following command in the terminal:

echo $0

The output should be the name of your active shell. In most cases this will be the bash shell unless you’ve changed it to zsh, ksh, or some other Linux shell.

Based on the shell used, you now need to edit its configuration file. Here’s what file to edit based on your shell:

Shell name Configuration file
bash ~ / .bashrc
ksh ~ / .kshrc
zsh ~ / .zshrc
csh ~ / .cshrc

In the case of a different shell, see the documentation to find out the name of the configuration file.

After specifying the file to edit, follow the steps below to set the $ PATH variable. In this guide, we’ll show you how to set up the $ PATH variable in the bash shell.

  1. Open Terminal and enter the following command to open the .bashrc File. You can use any Linux text editor of your choice.

    nano ~/.bashrc
  2. Use the following syntax to set the $ PATH variable.

    export PATH=$PATH:/path/to/directory/
  3. Save the file and update the current shell environment with source.

    source ~/.bashrc
  4. Enter the following command to check if the directory has been added or not.

    echo $PATH

If you entered the directory path correctly, it will appear in your system’s $ PATH, and you should be able to run all of the scripts within the folder from any directory on your system.

Linux also allows you to set the $ PATH variable permanently in a system-wide configuration so that anyone on your system can access and run the custom scripts in the specified directory.

To set $ PATH globally, you’ll need to add the directory using the same command you used when adding the temporary and permanent setup, but either in the / etc / environment or the / etc / profile File.

Open one of these files and append the path to the directory:

nano /etc/environment
nano /etc/profile

Successful setting of the $ PATH in Linux

Using the steps above, you can set your system’s $ PATH to look for scripts in directories that are otherwise unspecified.

In most cases, we recommend that you set the $ PATH variable on your system permanently in a user-specific configuration, as this is the most efficient of the two methods.

Also remember that you do not need to set the $ PATH variable in a system-wide configuration unless there is a specific program on your system that you want to access globally. The Locate and Find Commands in Linux are a lifesaver when you’re having trouble locating the source directory to add to the $ PATH variable.


Find folder Fold
How to find a directory on Linux

Looking for something? How to find the exact directory you need on Linux.

Continue reading


About the author

.



Source link

Share.

Leave A Reply