Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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/

share|improve this question
2  
You can use the variable $HOME for the home, and $USER for the user :) – dadexix86 Mar 4 at 14:28
2  
@dadexix86 you may as well post an answer. This question is far too cheap to waste time in the comments – Serg Mar 4 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 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 at 15:03
    
download was only for example. Files will be deleted after install. But thanks for the usefull info – FixXxeR Mar 4 at 15:06
up vote 20 down vote accepted

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.

share|improve this answer
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 at 18:10
3  
@asmeurer we only deal with Ubuntu and official derivatives. What default version does not use /home? – Rinzwind Mar 5 at 12:06
3  
Just because this site is only about Ubuntu doesn't mean you should avoid writing portable code and using best practices. – asmeurer Mar 7 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.
# 
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)
share|improve this answer
    
Wow thanks, that's nice – FixXxeR Mar 4 at 15:08
    
This is the real universal way, always works :) – Jacob Vlijm Mar 4 at 16:19
    
Just xdg-user-dir DOWNLOAD if you want to print it. :P – kos Mar 4 at 16:43
    
What does more do when !isatty(stdin)? – cat Mar 5 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 at 11:25

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.