11

I wonder how to access not only the variables defined in ~/.config/user-dirs.dirs with xdg-user-dir, e.g. "$(xdg-user-dir VIDEOS)", but also the following standard variables:

  1. XDG_CACHE_HOME:-$HOME/.cache
  2. XDG_CONFIG_HOME:-$HOME/.config
  3. XDG_DATA_HOME:-$HOME/.local/share
  4. XDG_RUNTIME_DIR:-"/run/user/$USER"
  5. XDG_CONFIG_DIRS:-/etc/xdg
  6. XDG_DATA_DIRS:-/usr/local/share:/usr/share

For that purpose I do the following in my ~/.bash_login file:

# Define standard directories.
declare -gx XDG_CACHE_HOME=~/.cache
declare -gx XDG_CONFIG_HOME=~/.config
declare -gx XDG_DATA_HOME=~/.local/share
declare -gx XDG_RUNTIME_DIR="/run/user/$USER"
declare -gx XDG_CONFIG_DIRS="$(IFS=: path /etc/xdg)"
declare -gx XDG_DATA_DIRS="$(IFS=: path /usr/local/share:/usr/share)"
# Source supplementary directories to export or overwrite existing standard ones.
declare a="$XDG_CONFIG_HOME/user-dirs.dirs"
if [[ -e $a ]]; then
  source "$a"
  declare b=""
  for b in ${!XDG_*}; do
    if [[ $b =~ ^XDG_[_[:alnum:]]+_DIR$ ]]; then
      declare -gx "$b"
    fi
  done
fi

Is there a mechanism to access the above directory and path variables other than the user directory variables defined by the "XDG" directory structure specification?

1
  • 1
    It's actually a number: XDG_RUNTIME_DIR:-"/run/user/$UID"
    – Tom Hale
    Sep 27, 2016 at 5:12

3 Answers 3

6

Those environment variables are all optional. If they are not set then your script must substitute the default values given in the specification itself.

someprog --cachedir "${XDG_CACHE_HOME:-$HOME/.cache}"
2
  • 3
    This optionality is one of the main reasons why I define them once as environment variables because I don't want to repeat the default value upon every "${XDG_CACHE_HOME:-~/.cache}" access. Optimally there should exist a similar mechanism as there is with "$(xdg-user-dir VIDEOS)" providing that default value implicitly. That way I don't have to remember the default value for each and every XDG variable.
    – Tim Friske
    Dec 6, 2012 at 18:43
  • 3
    @Tim Friske: Note that "${XDG_CACHE_HOME:-~/.cache}" is incorrect, one should use "${XDG_CACHE_HOME:-$HOME/.cache}" instead.
    – Flimm
    Sep 30, 2013 at 10:21
4

I believe systemd-path is widely available and it could be used to get paths with environment aware resolution and with proper fallbacks:

$ systemd-path user-shared
/home/Xerkus/.local/share

$ HOME=/foo systemd-path user-shared
/foo/.local/share
$ XDG_DATA_HOME=/bar systemd-path user-shared
/bar

From man:

systemd-path
may be used to query system and user paths. The tool makes many of the paths described in file-hierarchy(7) queriable. When invoked without arguments a list of known paths and their current values is shown. ...

0

Plus the defaults:

man xdg-user-dir => SEE ALSO: xdg-user-dirs-update(1)

man xdg-user-dirs-update=> SEE ALSO: user-dirs.defaults(5)

man user-dirs.defaults => user-dirs.defaults under first XDG_CONFIG_DIRS (default: /etc/xdg)

So:

read -r -d : <<<"${XDG_CONFIG_DIRS:=/etc/xdg}" XDG_USER_DIRS_DEFAULTS
XDG_USER_DIRS_DEFAULTS+=/user-dirs.defaults

You must log in to answer this question.

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