0

The tilde character ~ doesn't function as the home directory shortcut on Windows, unlike as on Unix systems. I've always used the character as the home path in written text for all operating systems and I wonder if this is factually wrong.

%USERPROFILE% may be more suitable as it can be used as an actual shortcut in the Windows Explorer.

2 Answers 2

3

The tilde character may have a totally different meaning in Windows, and I would consider it invalid – or at least ambiguous – to use it to refer to the user's home directory.

On file systems that do not support long file paths, the tilde would be used as a replacement character:

A long file name is considered to be any file name that exceeds the short MS-DOS (also called 8.3) style naming convention. When you create a long file name, Windows may also create a short 8.3 form of the name, called the 8.3 alias or short name, and store it on disk also.

[…]

On many file systems, a file name will contain a tilde (~) within each component of the name that is too long to comply with 8.3 naming rules.

You can read more about 8.3 filenames on Wikipedia, which is a legacy file naming convention that dates back to the times of DOS.

That said, modern programs may indeed use ~ as an alias for the user's home – PowerShell for example, see @Broam's answer. And of course there is a difference between having the user type something that the shell then interprets vs. what a shell may output. However, as you cannot assume users to use PowerShell, or be knowledgeable about Unix conventions, I'd recommend against it.

2
  • It's rare to find 13-year-old filenames, but at the same time I believe that modern command parsers that do support tilde (e.g., PowerShell and Bash) can handle ~ in filenames in that situation, as ~/ is usually the prefix and not part of the filename, (e.g. README~1.TXT)
    – Broam
    Oct 31, 2018 at 18:30
  • Yes, you're right – one must consider replacement that occurs after the user has entered a path with a tilde (similar to how Bash would expand it first, before the command actually sees a tilde). However, there's still some ambiguity, whereas under Unix, the tilde isn't used for anything else IIRC.
    – slhck
    Oct 31, 2018 at 18:31
2

For most cases, you are correct (which is immensely maddening when I'm on my work computer).

One exception: PowerShell accepts use of the ~ character and it is expanded to your home directory if you use tab completion.

Third-party Bash installs (such as the copy that comes with Git) support ~ as well.

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