DEV Community

Bijay Kumar Pun
Bijay Kumar Pun

Posted on

Linux Test Session: $PATH

To know pathname of file or link that is executed in current environment, use which

Alt Text

To add a path variable use export

Alt Text

we can also use . to add the current directory path to the PATH variable
$export PATH=$PATH:.

Path variable can both be prepended or appended, and they are separated by :

All changes, however, is temporary. These path variables are deleted when the user signs out, or when the shell is closed.

To make this permanent, the export command needs to be executed every time the system boots. This can be done in two distinct ways:

1.Via existing files:

when we login to the linux system, the login shell looks for five different startup files to process the commands from.

  • /etc/profile
    (for login shell)
    The main and the default startup file
    All users execute this file

  • $HOME/.bash_profile

  • $HOME/.bash_login

  • $HOME/.profile
    (interactive shell doesn't process .profile file)

  • $HOME/.bashrc
    (for interactive shell)
    This is run from one of the other above three files

When one of the above four files is encountered, it is executed first, while any file after that is ignored!

2.Via creating a new .sh file

Create a new .sh file inside /etc/profile.d/ with the export command. This file is executed every time /etc/profile is executed. One advantage of adding an environment variable this way is data will not be lost when system is updated. Eg. /etc/profile may be reset during system update.

Finally, execute source command to make the changes apparent to the current shell.
Eg.
source ~/.bash_profile
source ~/.bashrc

Bash shell can be started in three ways:

  1. default login shell at login time
  2. Interactive shell that is started by spawning a subshell
  3. Non-interactive shell to run a script

Top comments (0)