Linux Shell Environment Path: Difference between revisions

From Free Knowledge Base- The DUCK Project
Jump to navigation Jump to search
mNo edit summary
No edit summary
 
Line 1: Line 1:
The current path environmental variable can be viewed
The current PATH environment variable can be viewed with:
  echo $PATH
  echo $PATH
capital letters on PATH. Want to know if a command is in $PATH?
Note that PATH is in capital letters.
 
Want to know if a command is in $PATH?
  which command
  which command


Need to add a directory path to the shell path statement for your own scripts?
Need to add a directory to the shell PATH for your own scripts? Below are methods to modify the PATH, with a focus on Linux Mint and other Linux distributions.
* Per user shell
 
== Per-User Shell ==
You can modify the PATH for a single user by editing shell configuration files. The recommended approach for Linux Mint is to use <code>~/.bashrc</code>, as it is sourced for every interactive shell and works reliably.
 
=== Using ~/.profile ===
Edit the user's profile file:
  vi ~/.profile
  vi ~/.profile
Look for the line with a comment about "set PATH so it includes user's private bin directories" and add your custom path in there like this: ( added /myscripts to the path )
Look for the line with a comment about "set PATH so it includes user's private bin directories" and add your custom path, for example (adds <code>/myscripts</code> to the PATH):
  PATH="$HOME/bin:$HOME/.local/bin:/myscripts:$PATH"
  PATH="$HOME/bin:$HOME/.local/bin:/myscripts:$PATH"
note: In the past we used ~/.bash_profile and had to include "export PATH"
Save the file. Log out and back in for the changes to take effect. In Linux Mint, <code>~/.profile</code> is sourced for login shells, and <code>export PATH</code> is typically unnecessary as PATH is already exported.


Look at /etc/profile which is used for each new user directory created. It has a script still looking for .bash_profile so it appears that if a .bash_profile is created, the script will identify its existence and use it.
=== Using ~/.bashrc (Recommended) ===
 
For most Linux Mint users, modifying <code>~/.bashrc</code> is the preferred method because it applies to all interactive shells, not just login shells. Edit the file:
Note: '''Recommend you use the .bashrc method mentioned below this note!  It seems to work very well on most systems.'''
 
IF THAT DOESN'T WORK - THIS WILL ...
  vi ~/.bashrc
  vi ~/.bashrc
Add your custom path, for example:
  PATH="/myscripts:$PATH"
  PATH="/myscripts:$PATH"
Save the file and run <code>source ~/.bashrc</code> to apply changes immediately. No <code>export PATH</code> is needed in Linux Mint, as PATH is already exported in the shell environment. This method has been verified to work reliably on Linux Mint 21.1 and later.


Note: the .bashrc is executed every time a bash shell is opened and .bash_profile only if it's a login shell.
'''Note:''' In older systems, you might encounter references to <code>~/.bash_profile</code>. In Linux Mint, <code>~/.profile</code> is used instead for login shells, and <code>~/.bash_profile</code> is rarely needed unless explicitly created.


If you "sudo bash" then your custom path directories have vanished!  /etc/sudoers is configured to replace your PATH with a default one. You have a couple options, either remove Defaults secure_path= from /etc/sudoers or add your custom directory to the secure_path in /etc/sudoers. For some stupid reason when you visudo the /etc/sudoers file opens in nano!  stupid! 
== Handling PATH with Sudo ==
If you run <code>sudo bash</code>, your custom PATH directories may disappear. This is because <code>/etc/sudoers</code> is configured with a <code>secure_path</code> that overrides the user's PATH. To address this:
* Edit <code>/etc/sudoers</code> using:
  sudo visudo
* Add your custom directory to the <code>secure_path</code> line, for example:
  Defaults secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/myscripts"
* Alternatively, remove the <code>secure_path</code> line (not recommended for security reasons).
By default, <code>visudo</code> opens in <code>nano</code>. To change the default editor to <code>vi</code> or another editor:
  sudo update-alternatives --config editor
  sudo update-alternatives --config editor
Will allow you to fix the stupid editor default (idiots at Ubuntu and Mint don't you think nano is for dosy people?)
Select your preferred editor from the list.
  sudo visudo
 
== Adding to the PATH for All Users ==
To modify the PATH system-wide for all users, you can use one of the following methods.
 
=== Editing /etc/profile ===
Edit the system profile file:
vi /etc/profile
Add your custom path at the bottom, for example:
  PATH="$PATH:/your/path"
Save the file. No <code>export PATH</code> is needed, as PATH is typically exported earlier in <code>/etc/profile</code>. Changes take effect for new login shells.


Adding to the PATH for ALL USERS can be done like this:
=== Using /etc/profile.d/ (Recommended) ===
* edit /etc/profile, put the modified PATH in there. 
A more modular approach, common in Linux Mint, is to add a script to <code>/etc/profile.d/</code>:
  PATH=$PATH:/your/path; export PATH
vi /etc/profile.d/custom-path.sh
Add that line to the bottom of /etc/profile
Add the following:
  PATH="$PATH:/your/path"
Save the file and make it executable:
chmod +x /etc/profile.d/custom-path.sh
This method is cleaner and easier to manage, as it keeps customizations separate from <code>/etc/profile</code>. Changes apply to new login shells.


== Additional Notes ==
* The <code>~/.bashrc</code> method is particularly reliable in Linux Mint, as it ensures your PATH is updated for every terminal session.
* Always test your PATH after making changes by opening a new terminal and running <code>echo $PATH</code>.
* Avoid removing <code>secure_path</code> from <code>/etc/sudoers</code> unless absolutely necessary, as it enhances system security.


<small><small>keywords: bash path bashrc bash_profile bash_history command set</small></small>
<small><small>keywords: bash path bashrc profile sudo secure_path environment</small></small>

Latest revision as of 07:37, 14 May 2025

The current PATH environment variable can be viewed with:

echo $PATH

Note that PATH is in capital letters.

Want to know if a command is in $PATH?

which command

Need to add a directory to the shell PATH for your own scripts? Below are methods to modify the PATH, with a focus on Linux Mint and other Linux distributions.

Per-User Shell

You can modify the PATH for a single user by editing shell configuration files. The recommended approach for Linux Mint is to use ~/.bashrc, as it is sourced for every interactive shell and works reliably.

Using ~/.profile

Edit the user's profile file:

vi ~/.profile

Look for the line with a comment about "set PATH so it includes user's private bin directories" and add your custom path, for example (adds /myscripts to the PATH):

PATH="$HOME/bin:$HOME/.local/bin:/myscripts:$PATH"

Save the file. Log out and back in for the changes to take effect. In Linux Mint, ~/.profile is sourced for login shells, and export PATH is typically unnecessary as PATH is already exported.

Using ~/.bashrc (Recommended)

For most Linux Mint users, modifying ~/.bashrc is the preferred method because it applies to all interactive shells, not just login shells. Edit the file:

vi ~/.bashrc

Add your custom path, for example:

PATH="/myscripts:$PATH"

Save the file and run source ~/.bashrc to apply changes immediately. No export PATH is needed in Linux Mint, as PATH is already exported in the shell environment. This method has been verified to work reliably on Linux Mint 21.1 and later.

Note: In older systems, you might encounter references to ~/.bash_profile. In Linux Mint, ~/.profile is used instead for login shells, and ~/.bash_profile is rarely needed unless explicitly created.

Handling PATH with Sudo

If you run sudo bash, your custom PATH directories may disappear. This is because /etc/sudoers is configured with a secure_path that overrides the user's PATH. To address this:

  • Edit /etc/sudoers using:
 sudo visudo
  • Add your custom directory to the secure_path line, for example:
 Defaults secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/myscripts"
  • Alternatively, remove the secure_path line (not recommended for security reasons).

By default, visudo opens in nano. To change the default editor to vi or another editor:

sudo update-alternatives --config editor

Select your preferred editor from the list.

Adding to the PATH for All Users

To modify the PATH system-wide for all users, you can use one of the following methods.

Editing /etc/profile

Edit the system profile file:

vi /etc/profile

Add your custom path at the bottom, for example:

PATH="$PATH:/your/path"

Save the file. No export PATH is needed, as PATH is typically exported earlier in /etc/profile. Changes take effect for new login shells.

Using /etc/profile.d/ (Recommended)

A more modular approach, common in Linux Mint, is to add a script to /etc/profile.d/:

vi /etc/profile.d/custom-path.sh

Add the following:

PATH="$PATH:/your/path"

Save the file and make it executable:

chmod +x /etc/profile.d/custom-path.sh

This method is cleaner and easier to manage, as it keeps customizations separate from /etc/profile. Changes apply to new login shells.

Additional Notes

  • The ~/.bashrc method is particularly reliable in Linux Mint, as it ensures your PATH is updated for every terminal session.
  • Always test your PATH after making changes by opening a new terminal and running echo $PATH.
  • Avoid removing secure_path from /etc/sudoers unless absolutely necessary, as it enhances system security.

keywords: bash path bashrc profile sudo secure_path environment