In this lesson, you will learn how to make your Bash scripts "see" and interact with your graphical desktop environment.
When you run a script from a terminal emulator, it usually inherits your desktop's session information automatically. However, when you run a script via a background process—like a systemd timer or a cron job—the script loses its connection to your display. To "reach" your screen, the script needs to know which display session to talk to and which user is currently logged into that GUI.
The Display Environment Variables
The two primary variables responsible for this connection are DISPLAY and XAUTHORITY.
DISPLAY: Usually set to:0or:1, this tells the system which physical or virtual screen the graphical application should target.XAUTHORITY: This points to a file containing the credentials needed to authenticate with your X11 display server. Without this, the system will block your script from opening windows for security reasons.
Connecting Your Script
If you are writing a script that needs to pop up a notification or launch a GUI tool from a background service, you must explicitly export these variables. You can usually find the correct values by running env | grep -E 'DISPLAY|XAUTHORITY' in your terminal while logged into your desktop.
Here is a simple way to incorporate this into your scripts:
#!/bin/bash
# Define the display and authority for the current user
export DISPLAY=:0
export XAUTHORITY=/home/yourusername/.Xauthority
# Now you can interact with the desktop
notify-send "Hello!" "Your background script has reached the GUI."
Why does this matter?
If you try to run a command like gedit or notify-send from a service without these variables, the command will simply fail with an error like cannot open display. By manually bridging your script to the session environment, you gain the ability to provide visual feedback to the user, even when the logic is triggered by a non-interactive system process.
Key Takeaway: To interact with a graphical desktop from a non-interactive script, you must explicitly define the DISPLAY and XAUTHORITY environment variables to authenticate and route your commands to the active user session.