2

This seems like a simple question that every application developer would have, but I can't find the answer.

Per the latest XDG Basedir Spec, there are many directories that should be defined in environment variables. As one example:

There is a single base directory relative to which user-specific configuration files should be written. This directory is defined by the environment variable $XDG_CONFIG_HOME.

Let's say my Bash script saves a user config - and I want to follow the XDG spec - How do I find $XDG_CONFIG_HOME? It makes sense that you would do something like

#!/bin/bash

# Print out $XDG_CONFIG_HOME
echo Your XDG_CONFIG_HOME is: [$XDG_CONFIG_HOME]

But running this on a fresh Ubuntu and Fedora Silverblue install both give the output:

$ ./test.sh
Your XDG_CONFIG_HOME is: []

Which seems totally useless, since that would mean on fresh desktop installs of Ubuntu and Fedora Silverblue, my app would write config files into the running directory. Yet, I can see so many applications properly putting the data in ~/.config/. Where are these apps finding the XDG environment variables, and how can I find them too?

1 Answer 1

2

The spec explains how to handle missing values:

If $XDG_DATA_HOME is either not set or empty, a default equal to $HOME/.local/share should be used.

and so on for all the XDG variables.

As a result of this, desktop environments often only set the values when their do not match the default. In your scripts, use expansions such as ${XDG_CONFIG_HOME:-$HOME/.config} with the appropriate defaults for each variable.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .