Skip to content

Instantly share code, notes, and snippets.

@jakub-g
Last active February 2, 2026 10:25
Show Gist options
  • Select an option

  • Save jakub-g/35f93f7af9b7879a2ef7024dd943170a to your computer and use it in GitHub Desktop.

Select an option

Save jakub-g/35f93f7af9b7879a2ef7024dd943170a to your computer and use it in GitHub Desktop.
Alternative to `source ~/.zshrc`
export PATH=$(zsh -l -c 'echo $PATH')

or shell agnostic

# 1. Detect the parent process name
parent_shell=$(ps -p $PPID -o comm= | tr -d '-') # tr removes the leading dash if it's a login shell

# 2. Map the process name to a valid binary path
# We use 'command -v' to ensure the shell actually exists on the system
if command -v "$parent_shell" >/dev/null 2>&1; then
    shell_bin="$parent_shell"
else
    # Fallback to the system's default SHELL if detection fails
    shell_bin="${SHELL:-/bin/sh}"
fi

# 3. Capture the PATH as it would be in a fresh login session
# We use -l (login) and -c (command)
fresh_path=$($shell_bin -l -c 'echo $PATH' 2>/dev/null)

# 4. Apply it if we successfully got a result
if [ -n "$fresh_path" ]; then
    export PATH="$fresh_path"
    echo "Environment synced with $shell_bin login profile."
else
    echo "Warning: Could not refresh PATH from parent shell. Using current PATH."
fi
@jakub-g
Copy link
Author

jakub-g commented Feb 2, 2026

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment