0

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.

1 Answers1

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.