I'm new to Bash and need an Ubuntu/Linux script for an installation process. It should locate the current user's Downloads directory whether it is named "Downloads" or the French equivalent "Téléchargements", then store the full path in a variable such as DOWNLOAD_DIR. The username is not known in advance, so the script should determine the user's home directory safely. I'll use the resulting path to download files and then run commands such as changing into that directory and installing a downloaded package.
3 Answers
Use `$HOME` rather than constructing a path from `$USER`. `$HOME` is already the current user’s home directory, and it may not always be `/home/$USER`. Always quote it in case the path contains spaces.
If you only need to support these two names, you can write:
`if [ -d "$HOME/Downloads" ]; then`
` DOWNLOAD_DIR="$HOME/Downloads"`
`elif [ -d "$HOME/Téléchargements" ]; then`
` DOWNLOAD_DIR="$HOME/Téléchargements"`
`else`
` echo "Could not find a Downloads directory" >&2`
` exit 1`
`fi`
After that, use `"$DOWNLOAD_DIR"` whenever you refer to the directory.
`$USER` is an environment variable and can technically be changed, so it isn’t the best value to use for identifying an account. If you truly need the login name, use `$(id -un)`. However, for this task you don’t need the username at all—`$HOME` is simpler and safer.
For an installation script, avoid assuming that the download directory exists. You could create it if appropriate:
`DOWNLOAD_DIR="$(xdg-user-dir DOWNLOAD)"`
`mkdir -p "$DOWNLOAD_DIR" || exit 1`
`cd "$DOWNLOAD_DIR" || exit 1`
Then reference the downloaded file with a quoted path, for example `sudo dpkg -i "$DOWNLOAD_DIR/chrome.deb"`.
On Linux, the locale-aware way is to use `xdg-user-dir`, which reads the user’s configured directory names:
`DOWNLOAD_DIR="$(xdg-user-dir DOWNLOAD)"`
Then you can use it safely with quotes:
`cd "$DOWNLOAD_DIR" || exit 1`
For example, check that the directory exists before continuing:
`if [ -d "$DOWNLOAD_DIR" ]; then`
` echo "Downloads directory: $DOWNLOAD_DIR"`
`else`
` echo "No Downloads directory found" >&2`
` exit 1`
`fi`
This handles translated folder names without manually listing every possible translation.

Thanks, I’m using Ubuntu Linux. Using `$HOME` and the locale-aware `xdg-user-dir DOWNLOAD` command gives me exactly the variable I need for the installer.