34.6 C
Jaipur
Tuesday, June 22, 2021

Add directory to path in Linux – Linux Hint

Must read

Every time you run a command on a Linux terminal, you are basically commanding the shell to run an executable program bearing the given name. Executable programs, including simple programs such as ls, mkdir, touch, and find, reside on special directories on the filesystem. In Linux, directories that accommodate such executable programs include:

/usr/sbin
/bin/usr/local/bin
/usr/local/bin
/bin
/usr/bin
/snap/bin
/sbin
/usr/games
/usr/local/games

A file with executable permissions in one of those directories can be executed from any location within the Linux terminal.

So, the question begs, how does the Linux shell know where to look for the programs? It doesn’t start the search from the current directory or anywhere random in the filesystem. The shell relies on the $PATH variable.

What is the $PATH variable?

$PATH is an environment variable that tells the shell where to locate the executable file. There are various directories defined in the $PATH variable. To display directories in your $PATH, run the command:

To locate the directory where a command executable is located, use the which command as follows

For example, to locate where the executable for the pwd command, run the command:

From the output, we can see that the executable resides in the /bin directory. For the touch command, the executable resides in the /usr/bin directory.

How to add a directory to $PATH

Suppose you have a simple shell script called greetings.sh that prints “Hello World” placed in the /home/james/files directory. By default, the directory is not yet defined in the $PATH variable. To run the script from any location or directory, you need to specify the absolute path to the script. Now, this can be cumbersome and time-consuming.

To run the script globally (regardless of your location in the filesystem) without specifying the full path to the script, you need to add the directory containing the script to the $PATH variable using the syntax below.

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

In this case, the command will be:

$ export PATH=$PATH:/home/james/files

You should now be in a position to call or run the script from any directory within your Linux system without specifying the absolute path to the script as shown.

How to permanently add the directory to the $PATH variable

The path that we have just defined to $PATH is only temporary and doesn’t persist when you close the terminal or reboot your system. It only works in the current shell session. If you exit and launch another session, you will bump into the error as shown.

To persist the changes, define the $PATH variable in the ~.bashrc configuration file. To accomplish this, open the configuration file.

Then add the line as shown.

$ export PATH=”$PATH:/home/james/files”

After that, save and exit. To load the new changes, invoke the source command as follows:

To verify the addition of the directory to $PATH, execute the command as shown.

Wrapping up

And there you have it, guys! We have managed to add the directory to $PATH on Linux successfully. As you have seen, it’s quite convenient and straightforward, especially if you will be calling the script or application regularly from the shell. The same commands will work for any Linux flavor.

Source link

- Advertisement -

More articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisement -

Latest article