10

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
    Commented Mar 4, 2016 at 14:28
  • 2
    @dadexix86 you may as well post an answer. This question is far too cheap to waste time in the comments Commented Mar 4, 2016 at 14:30
  • @Serg yes, but I am almost sure that it is a duplicate, and I am looking for the original one :)
    – dadexix86
    Commented Mar 4, 2016 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
    Commented Mar 4, 2016 at 15:03
  • download was only for example. Files will be deleted after install. But thanks for the usefull info
    – FixXxeR
    Commented Mar 4, 2016 at 15:06

3 Answers 3

23

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
    Commented Mar 4, 2016 at 18:10
  • 3
    @asmeurer we only deal with Ubuntu and official derivatives. What default version does not use /home?
    – Rinzwind
    Commented Mar 5, 2016 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
    Commented Mar 7, 2016 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 :) Commented Mar 4, 2016 at 16:19
  • Just xdg-user-dir DOWNLOAD if you want to print it. :P
    – kos
    Commented Mar 4, 2016 at 16:43
  • What does more do when !isatty(stdin)?
    – cat
    Commented Mar 5, 2016 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
    Commented Mar 5, 2016 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. Commented Mar 5, 2016 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

You must log in to answer this question.

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