Linux: Change the PATH [ Add New Directory ]
To add a new directory to the PATH environment variable in Linux, you can follow these steps:
- Open a terminal window.
- Check your current
PATHby running the following command:echo $PATHThis will display a list of directories separated by colons. These directories are searched in order when you type a command in the terminal.
- Create a new directory that you want to add to the
PATH. For example, you might create a directory calledbinin your home directory:mkdir ~/bin - Add the new directory to your
PATHby editing your shell startup file. The exact file you need to edit will depend on which shell you're using. For example, if you're using the Bash shell, you would edit your~/.bashrcfile:nano ~/.bashrcAdd the following line at the end of the file:
export PATH=$PATH:~/binThis appends the new directory to the existing
PATHvariable, separated by a colon. - Save and close the file. In nano, you can do this by pressing
Ctrl+X, thenY, thenEnter. - Reload your shell startup file by running the following command:
source ~/.bashrcThis will apply the changes you made to your
PATHenvironment variable. - Verify that the new directory has been added to your
PATHby running the following command:echo $PATHThis should display the original directories in your
PATH, followed by the new directory you added.
~/bin in this example) and run them from any location in the terminal.