Skip to content

Latest commit

 

History

History
109 lines (75 loc) · 2.68 KB

clean_environment_variables.md

File metadata and controls

109 lines (75 loc) · 2.68 KB

Clean Environment Variable Management for PATH and PYTHONPATH

This guide explains how to manage PATH and PYTHONPATH effectively in Linux, avoiding redundancy and ensuring clean configurations. It’s a practical solution for anyone frequently working with custom binaries or Python packages.


Key Features:

  1. Prevent redundant entries in PATH and PYTHONPATH.
  2. Add directories conditionally to these environment variables.
  3. Clean existing redundancies in a safe and reusable manner.

Adding Directories Conditionally to PATH

Example:

Add /path/to/directory to PATH only if it’s not already included:

if [[ ":$PATH:" != *":/path/to/directory:"* ]]; then
    export PATH="/path/to/directory:$PATH"
fi

Deduplicate PATH:

Remove redundant entries from PATH:

export PATH=$(echo "$PATH" | awk -v RS=':' '!seen[$0]++ {printf "%s%s", sep, $0; sep=":"}')

Adding Directories Conditionally to PYTHONPATH

Example:

Add /path/to/python/package to PYTHONPATH only if it’s not already included:

if [[ ":$PYTHONPATH:" != *":/path/to/python/package:"* ]]; then
    export PYTHONPATH="$PYTHONPATH:/path/to/python/package"
fi

Clean Leading/Trailing Colons:

Remove leading or trailing colons from PYTHONPATH:

export PYTHONPATH=$(echo "$PYTHONPATH" | sed 's/^://; s/:$//')

Deduplicate PYTHONPATH:

Remove redundant entries from PYTHONPATH:

export PYTHONPATH=$(echo "$PYTHONPATH" | awk -v RS=':' '!seen[$0]++ {printf "%s%s", sep, $0; sep=":"}')

Example .bashrc Configuration

Here’s an example .bashrc configuration for managing both PATH and PYTHONPATH cleanly:

# >>> Manage PATH >>>
if [[ ":$PATH:" != *":/desired/path/to/bin:"* ]]; then
    export PATH="/desired/path/to/bin:$PATH"
fi
export PATH=$(echo "$PATH" | awk -v RS=':' '!seen[$0]++ {printf "%s%s", sep, $0; sep=":"}')
# <<< Manage PATH <<<

# >>> Manage PYTHONPATH >>>
export PYTHONPATH=$(echo "$PYTHONPATH" | sed 's/^://; s/:$//')  # Clean leading/trailing colons
if [[ ":$PYTHONPATH:" != *":/path/to/python/package:"* ]]; then
    export PYTHONPATH="$PYTHONPATH:/path/to/python/package"
fi
export PYTHONPATH=$(echo "$PYTHONPATH" | awk -v RS=':' '!seen[$0]++ {printf "%s%s", sep, $0; sep=":"}')
# <<< Manage PYTHONPATH <<<

# Other configurations...

Verifying the Results

Check PATH:

echo $PATH

Check PYTHONPATH:

echo $PYTHONPATH

Test Commands:

which <command>

Summary

By following this guide:

  • Your PATH and PYTHONPATH will remain clean and redundant-free.
  • Updates to these environment variables will be efficient and conflict-free.
  • The logic is reusable for other environment variables.