9

I want to make an install script for my home PC and I want to make it more flexible.

How to replace my username with current user variable?

Example: download to: /home/THISUSER/Downloads/

5
  • 2
    You can use the variable $HOME for the home, and $USER for the user :)
    – dadexix86
    Mar 4 '16 at 14:28
  • 2
    @dadexix86 you may as well post an answer. This question is far too cheap to waste time in the comments Mar 4 '16 at 14:30
  • @Serg yes, but I am almost sure that it is a duplicate, and I am looking for the original one :)
    – dadexix86
    Mar 4 '16 at 14:31
  • Note that the Downloads folder only exists on English systems. They can get renamed if the user chose a different localization.
    – Byte Commander
    Mar 4 '16 at 15:03
  • download was only for example. Files will be deleted after install. But thanks for the usefull info
    – FixXxeR
    Mar 4 '16 at 15:06
22

You can use the variable $HOME for the home, and $USER for the user.

Your example can then be $HOME/Downloads or /home/$USER/Downloads.

3
  • 10
    Not all systems use /home for home directories, so the best practice is to use $HOME if you are referencing the home directory.
    – asmeurer
    Mar 4 '16 at 18:10
  • 3
    @asmeurer we only deal with Ubuntu and official derivatives. What default version does not use /home?
    – Rinzwind
    Mar 5 '16 at 12:06
  • 4
    Just because this site is only about Ubuntu doesn't mean you should avoid writing portable code and using best practices.
    – asmeurer
    Mar 7 '16 at 21:18
15

If you want to do this you should use ...

more ~/.config/user-dirs.dirs
# This file is written by xdg-user-dirs-update
# If you want to change or add directories, just edit the line you're
# interested in. All local changes will be retained on the next run
# Format is XDG_xxx_DIR="$HOME/yyy", where yyy is a shell-escaped
# homedir-relative path, or XDG_xxx_DIR="/yyy", where /yyy is an
# absolute path. No other format is supported.
# 
XDG_DESKTOP_DIR="/discworld/Desktop"
XDG_DOWNLOAD_DIR="/discworld/Downloads"
XDG_TEMPLATES_DIR="/discworld/Templates"
XDG_PUBLICSHARE_DIR="/discworld/Public"
XDG_DOCUMENTS_DIR="/discworld/Documents"
XDG_MUSIC_DIR="/discworld/Music"
XDG_PICTURES_DIR="/discworld/Pictures"
XDG_VIDEOS_DIR="/discworld/Videos"

so that would be

echo $(xdg-user-dir DOWNLOAD)

and it will show the default download location (in my case /discworld/Downloads). Works for all of these words. Like ...

echo $(xdg-user-dir DESKTOP)
11
  • This is the real universal way, always works :) Mar 4 '16 at 16:19
  • Just xdg-user-dir DOWNLOAD if you want to print it. :P
    – kos
    Mar 4 '16 at 16:43
  • What does more do when !isatty(stdin)?
    – cat
    Mar 5 '16 at 2:53
  • @JacobVlijm There are systems where xdg-user-dirs isn't installed. Probably less so under Ubuntu, but it's definitely less universal than using $HOME and $USER.
    – jazzpi
    Mar 5 '16 at 11:25
  • @jazzpi Since we are on Ask Ubuntu, the above user's directories within the scope of the question are universal, while ` $HOME/Downloads` definitely is not. Mar 5 '16 at 12:31
0

You could always use tilde (~) as the home directory, so something like:

cd ~/Downloads/

Would be the same as

cd /home/username/Downloads/

This would only work for the current logged in user

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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