GNU bash, version 4.3.48(1)-release (x86_64-pc-linux-gnu), Ubuntu 16.04.3 LTS.

I need to obtain a value of DBUS_SESSION_BUS_ADDRESS and save it to a file.

If I run set | grep DBUS_SESSION_BUS_ADDRESS > /home/user/.DBUS_temp in Bash I have something similar to DBUS_SESSION_BUS_ADDRESS=unix:abstract=/tmp/dbus-N1wmwpEVBj in the .DBUS_temp file which is OK.

But when I try to run the command in script the following happens:

  1. An empty file is created if set | grep DBUS_SESSION_BUS_ADDRESS > /home/user/.DBUS_temp is used.
  2. BASH_EXECUTION_STRING='set | grep DBUS_SESSION_BUS_ADDRESS > /home/user/.DBUS_temp' is in the file if su -c 'set | grep DBUS_SESSION_BUS_ADDRESS > /home/user/.DBUS_temp' user is used.

What is wrong in my code/implementation?

Thank you for your time.

  • 2
    There's no reason to set | grep. Just echo "$DBUS_SESSION_BUS_ADDRESS" > file would work better in most case. Here, are you running the script in cron?
    – muru
    Nov 22, 2017 at 2:49
  • See askubuntu.com/questions/742870/…
    – muru
    Nov 22, 2017 at 2:52
  • @muru Thanks for your reply. I put the line into /etc/rc.local for it is run every time I switch on my PC.
    – pyramidka
    Nov 22, 2017 at 2:52
  • 2
    /etc/rc.local is waay too early for that. This variable is set when you login from the GUI.
    – muru
    Nov 22, 2017 at 2:54
  • @muru Thanks again for your kind comment and the link. I'll dive into it.
    – pyramidka
    Nov 22, 2017 at 2:58

1 Answer 1


I post this answer to my own question to close it.

Thanks to help of muru and the answer to this question I figured out how to obtain the DBUS_SESSION_BUS_ADDRESS variable in the form of DBUS_SESSION_BUS_ADDRESS=unix:abstract=/tmp/dbus-N1wmwpEVBj which is suitable for my purpose (using this I don't even need to save the variable to the file):

PID=$(pgrep gnome-session)
export $(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$PID/environ)

Using code from the mentioned answer ends up in obtaining just unix:abstract=/tmp/dbus-N1wmwpEVBj part of the DBUS_SESSION_BUS_ADDRESS variable. It does not work for me.

  • "Using code from the mentioned answer ends up in obtaining just "... That's why it does export DBUS_SESSION_BUS_ADDRESS=....
    – muru
    Nov 24, 2017 at 2:18
  • @muru Thanks again for your clarification. I'm not skilled Bash user and your suggestions were to the point.
    – pyramidka
    Nov 24, 2017 at 2:34

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.