Ora

How do I add to a Linux path?

Published in Linux PATH Configuration 6 mins read

The Linux PATH is an essential environment variable that tells your shell where to look for executable programs when you type a command. To add a directory to your Linux PATH, you can do so either temporarily for the current session or permanently by editing configuration files.

Understanding the Linux PATH Variable

The PATH variable is a colon-separated list of directories. When you type a command, your shell searches these directories in order, from left to right, to find the corresponding executable file. If the command isn't found in any of these directories, the shell returns a "command not found" error.

You can view your current PATH with the command:

echo $PATH

Adding to the PATH Temporarily

Adding a directory to your PATH temporarily means the change will only apply to your current shell session. Once you close the terminal or log out, the change will be lost. This method is useful for testing new tools or when you only need a specific program for a short period.

To add a directory temporarily, use the export command:

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

Explanation:

  • export: Marks the variable to be passed to child processes.
  • /path/to/your/directory: Replace this with the actual absolute path of the directory you want to add.
  • :: The colon acts as a separator between directories in the PATH variable.
  • $PATH: This expands to your current PATH variable, ensuring you append the new directory without overwriting existing entries.

Example:

If you have a custom script in /opt/my-scripts and want to run it from any directory in your current session:

export PATH="/opt/my-scripts:$PATH"

Now, your shell will first look for executables in /opt/my-scripts before searching other default directories.

Adding to the PATH Permanently

For changes to persist across reboots and new shell sessions, you need to edit a shell configuration file. The most common files for this are .bashrc (for Bash shells) or .profile (for login shells), usually located in your home directory.

1. For Bash Users (.bashrc)

The .bashrc file is executed every time you open a new interactive Bash shell. This is typically the most common place to add custom PATH entries for personal use.

Steps:

  1. Open .bashrc for editing:

    nano ~/.bashrc

    (You can use vi, gedit, or any other text editor.)

  2. Add the export line: Scroll to the end of the file and add a line similar to this:

    # Add custom directory to PATH
    export PATH="/path/to/your/directory:$PATH"

    It's good practice to add a comment explaining your change.

  3. Save and close the file.

  4. Apply the changes: To make the changes effective in your current session without logging out or restarting, source the file:

    source ~/.bashrc
    # Or simply: . ~/.bashrc

    New terminal sessions will automatically pick up the changes.

Example:

To permanently add /usr/local/bin/my-tools to your PATH:

# nano ~/.bashrc
export PATH="/usr/local/bin/my-tools:$PATH"

2. For Login Shells (.profile or .bash_profile)

The .profile (or .bash_profile on some systems) file is executed when you log in to your system (e.g., at the graphical login screen or via SSH). It's generally used for settings that should apply to your entire user environment, including non-interactive shells.

  • If you have a .bash_profile, it's often preferred over .profile for Bash users as it's typically read first. If .bash_profile exists, .profile might not be read.
  • Often, .bash_profile or .profile will source .bashrc for interactive shell settings.

Steps:

  1. Open the appropriate file:

    nano ~/.profile
    # Or: nano ~/.bash_profile
  2. Add the export line: Add the line near the end of the file.

    # Add custom directory to PATH
    export PATH="/path/to/your/directory:$PATH"
  3. Save and close the file.

  4. Apply changes: You'll typically need to log out and log back in for changes in .profile or .bash_profile to take full effect across all sessions. You can also source it for the current session:

    source ~/.profile

3. For Zsh Users (.zshrc)

If you use Zsh (Z Shell) instead of Bash, you should modify your .zshrc file, which is equivalent to Bash's .bashrc.

Steps:

  1. Open .zshrc:

    nano ~/.zshrc
  2. Add the export line:

    # Add custom directory to PATH
    export PATH="/path/to/your/directory:$PATH"
  3. Save, close, and source:

    source ~/.zshrc

Important Security Consideration: Avoid Leading Colons

It's crucial to exercise caution and avoid adding a leading colon (: or ::) when modifying your PATH variable.

  • A leading colon (:) at the beginning of the PATH definition (e.g., PATH=:/new/path:$PATH) or an empty path component (e.g., PATH=/usr/bin::/usr/local/bin) effectively includes the current working directory in your search path.
  • This poses a significant security risk. If an attacker can place a malicious executable with a common command name (like ls, cat, or grep) in a directory where you frequently work, and that directory is searched first due to the leading colon, your system could execute the malicious program instead of the legitimate one.

Always ensure your PATH additions explicitly define directories without relying on empty components that imply the current directory.

Verifying Your PATH Changes

After making any changes, always verify them by echoing the PATH variable:

echo $PATH

Confirm that your new directory is included in the output.

Where to Place New PATH Entries

When you add a directory, you can place it either at the beginning or the end of the PATH:

  • export PATH="/new/path:$PATH": Places the new directory at the beginning. The shell will search this directory first. Use this if you want your custom versions of commands to take precedence over system versions.
  • export PATH="$PATH:/new/path": Places the new directory at the end. The shell will search this directory last, after all system default paths. Use this if you want system commands to take precedence and your custom tools to be found only if no system command matches.

Comparison of PATH Modification Methods

Feature Temporary (export in terminal) Permanent (export in config file)
Persistence Only for the current shell session Persists across reboots and new sessions
Scope Current shell and its child processes All sessions started after the config file is read
Effort Quick and easy Requires editing a file and sourcing/relogging
Use Case Testing, one-time tasks Regular use of custom tools or applications
File No file edited .bashrc, .profile, .zshrc, etc.

Further Reading