Yes, I know that this question is a bit confusing, I just mean "the variables which are acessible from session's command line".

I am writing an application which runs automatically by upstart on filesystem, to listen to DBus login1 events and thus It runs from root. But after any user logs in I need to listen to his session bus for additional signals. That's why I want to get DBUS_SESSION_BUS_ADDRESS of current user.

Other methods of getting DBUS_SESSION_BUS_ADDRESS don't work for me, for example ~/.dbus/session-bus/* files are somehow outdated(?) ie their content does not match one in the environment.

  • if you know of a process id for the user, you can look in /proc/<pid>/environ for all the env var=value (each terminated by null).
    – meuh
    Oct 8, 2015 at 16:43

2 Answers 2


It sounds like you are trying to find the variable that a user has in their environment dynamically from an already running script as each process gets created?

A method I have used to solve this is to find the PID, then use the PID to parse that processes environment variables from /proc/PID/environ. This only works if the PID inherits the variables.

pid=1234         # replace with pid finding method
unset thisbus    # in case this snippet gets pasted into a loop
[ -f /proc/$pid/environ ] && thisbus="$(sed 's/^.*\x0DBUS_SESSION_BUS_ADDRESS=\([^\x0]*\)\x0.*$/\1/' /proc/$pid/environ)"
[ -z "$thisbus" ] && echo "not found" || echo "$thisbus"
  • Alternative implementations here and here
    – Luciano
    Sep 8, 2018 at 12:58

Besides the obvious echo

[sheepdog@dogpoung ~]$ echo $DBUS_SESSION_BUS_ADDRESS

In bash the export action shows you all(?) of the variables.

export - Names of exported shell variables. May also be specified as -e.

[sheepdog@dogpoung ~]$ export
declare -x COLORTERM="gnome-terminal"
declare -x DBUS_SESSION_BUS_ADDRESS="unix:abstract=/tmp/dbus-cdhvJBSPVi,guid=5451e8f0f49db972ccc8f845560efd7e"
declare -x DESKTOP_SESSION="gnome-classic"
declare -x DISPLAY=":0"
declare -x GDMSESSION="gnome-classic"
declare -x GDM_LANG="en_US.utf8"
declare -x GJS_DEBUG_OUTPUT="stderr"
  • Why this answer was given a -1? Just curious; personal growth and all that.
    – 0xSheepdog
    Oct 8, 2015 at 20:37
  • I think what the OP wants is to get the environment variables form another session (could be another user but also same user, with another X session running). This answer only prints out the environment variables in the current session.
    – Luciano
    Sep 8, 2018 at 12:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.