Reputation: 320
I am writing an script and I need to locate the desktop folder of the current user. I want it to be language-independent so I found that on Ubuntu you can find the path of the desktop (independently of the system language) in ~/.config/user-dirs.dirs
in the line beginning with the tag XDG_DESKTOP_DIR
.
I can capture this information easily using DESK=$(more ~/.config/user-dirs.dirs | grep "XDG_DESKTOP_DIR" | cut -d '"' -f2)
and obtain the path of the Desktop, but the problem is that I obtain something like this: $HOME/Desktop
and I need to expand the $HOME variable.
I was doing it with eval
but I know that is bad coding so I decided to change it.
So basically I am trying to expand the variable that is inside a variable that contains a mixture of text and variables. I would like to have a solution for this that is not hardcoded for the $HOME
variable, because this is not the first time that I find this issue.
I already tried ${!DESK}
, ${${DESK}}
(I can imagine why this is not working), [[ ${escri} ]]
and others in combination with echo -e
.
I would be super grateful if someone can help me with this issue since I remember that I already had this problem the first time that I wrote the code, that's why I gave up and used the wicked eval
.
Upvotes: 0
Views: 109
Reputation: 19545
The most useful way is to source ~/.config/user-dirs.dirs
. ~/.config/user-dirs.dirs
# Variable is now readily available
echo "$XDG_DESKTOP_DIR"
Another way is to use envsubst
to expand the environment variables before parsing the file:
desktop_dir="$(
envsubst < ~/.config/user-dirs.dirs |
sed -n 's/XDG_DESKTOP_DIR="\(.*\)"/\1/p'
)"
Upvotes: 5