Skip to content

Instantly share code, notes, and snippets.

@tataranovich

tataranovich/grabenv

Last active Aug 23, 2020
Embed
What would you like to do?
Grab environment
#!/bin/sh
XPROC="dbus-launch"
XPID=""
TARGET="$1"
if [ ! -z "$TARGET" ]; then
# Target is specified, check is it PID or process name
if echo "$TARGET" | grep -Eq '^[0-9]+$'; then
# Target is number, use it as PID
XPID="$TARGET"
XPROC=""
else
# Target is not number, use it as process name
XPROC="$TARGET"
fi
fi
if [ -z "$XPID" ]; then
# Find PID for process name started by current user
XPID=$(pgrep -u "$(whoami)" "$XPROC" | head -n 1)
fi
if [ ! -r "/proc/$XPID/environ" ]; then
echo "Failed to grab environment from $XPROC($XPID)" >&2
exit 1
else
xargs -r0 -n1 echo export < "/proc/$XPID/environ"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment