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/

  • 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

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.

  • 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

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.

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)
  • 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

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.