8

The ~ character is a universal alias in the Unix word for referencing the home directory of the user. It is a special character that will be parsed and replaced by the full path of the current user's home directory. Is there an equivalent of this in cmd.exe ? (not Powershell)

adamency
  • 300
  • 1
  • 7
  • 2
    Not really.. there is %UserProfile% which is similar to $HOME. – Señor CMasMas Sep 23 '21 at 00:16
  • Does this answer your question? [Change to user folder from the Command Prompt or PowerShell?](https://superuser.com/questions/1221640/change-to-user-folder-from-the-command-prompt-or-powershell) – phuclv Sep 23 '21 at 02:39
  • 3
    duplicates: [What is the equivalent of Linux's "~" (tilde) in Windows?](https://superuser.com/q/332871/241386), [Is there a shortcut command in Windows command prompt to get to the current user's home directory like there is in Linux?](https://superuser.com/q/168714/241386), [Is there a windows equivalent to using ~/ as a shorthand symbolizing your home/user directory?](https://superuser.com/q/476274/241386), [cmd equivalent to "cd ~" to change to `C:\Users\\Documents`](https://superuser.com/q/1048579/241386) – phuclv Sep 23 '21 at 04:15
  • 3
    Does this answer your question? [What is the equivalent of Linux's "~" (tilde) in Windows?](https://superuser.com/questions/332871/what-is-the-equivalent-of-linuxs-tilde-in-windows) – Herohtar Sep 23 '21 at 19:32
  • all the duplicates show the equivalent in cmd. not powershell, did you even read them? – phuclv Mar 05 '22 at 13:31

1 Answers1

18

No.

You can use commands like cd /D %homedrive%\%homepath% or cd /D %userprofile% but typing them is just not the same even if end result is. The closest to the simplicity of cd ~ I've ever seen is Señor CMasMas's elegant solution below.

Create a new bat file with one single line:

@cd /d %UserProfile% –

Save it with name cd~.bat into any folder in your %PATH%. After that you can get from anywhere in the system back to home directory by typing command

cd~
phuclv
  • 26,555
  • 15
  • 113
  • 235
Peregrino69
  • 4,526
  • 2
  • 22
  • 30
  • 13
    It is funny that you mention this. I have a `cd~.bat` file in my path that has one line in it.. `@cd /d %UserProfile%` – Señor CMasMas Sep 23 '21 at 02:18
  • 1
    That's the most elegant solution I've ever seen :D – Peregrino69 Sep 23 '21 at 09:35
  • 6
    Be aware that `%homepath%` does not include the drive, just the path relative to that drive. `%homedrive%` contains the drive letter. – Ross Presser Sep 23 '21 at 12:14
  • @RossPresser Good point, let's update the answer :-) – Peregrino69 Sep 23 '21 at 12:16
  • Batch file from batch file should be called via `call` command. – pbies Sep 23 '21 at 20:45
  • 1
    In Unix you wouldn't even type `cd ~`, just `cd` because `$HOME` is already the default destination. `~` is useful when you're cd'ed somewhere else and want to reference a file in your home dir, e.g. `vim ~/notes.txt` or `cp -a foo ~/bin/`. (Or in a script where you *might* be.) Anyway, isn't `%homedrive%\%homepath%` basically equivalent to `$HOME`, expanding an environment variable? When you say it's "not the same", do you mean it's too long to type? – Peter Cordes Sep 23 '21 at 22:30
  • @PeterCordes Yap, I generally drop the tilde except in scripts, `cd ~` just has a bit of charming nostalgia :-) And of course written down `cd` and `cd ~` don't mean the same thing. `%homedrive%\%homepath%` as concept is more `/users/$USER`, `%UserProfile%` is more `$HOME`, methinks. I updated the answer again. – Peregrino69 Sep 23 '21 at 23:07
  • In Unix, `cd` and `cd ~` *do* mean the same thing. Tilde expansion happens after word splitting, so even if your `$HOME` contains spaces, it still works like `cd "$HOME"` instead of breaking. Maybe you meant in Windows? I don't know Windows `cmd.exe` very well. – Peter Cordes Sep 23 '21 at 23:15
  • They do, _typed in shell_, but "typed in shell" != "written down". Writing on a paper, forum etc. `cd` = "change directory", but `cd ~` = "change directory to $HOME". That's what I meant :-) – Peregrino69 Sep 23 '21 at 23:25