Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer

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}"
share|improve this answer
    
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 '12 at 18:43
    
@Tim Friske: Note that "${XDG_CACHE_HOME:-~/.cache}" is incorrect, one should use "${XDG_CACHE_HOME:-$HOME/.cache}" instead. –  Flimm Sep 30 '13 at 10:21
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.