2

Why does the following command: mkdir -p '~/something' creates a directory directory named as '~' in the current, instead of creating a directory named as something in the home directory? Is there any way to fix this?

ps. I get the value ~/something as an environment variable wrapped in single quotation.

Gandalf
  • 336
  • 2
  • 6
  • 13
  • 3
    ~ is not expanded when quoted: therefore the system is not able to translate it as "my home folder". If you want to use the quotes (for instance, because there are whitespaces in the folder path), better use double quotes replacing the tilde sign with the variable $HOME. Summarizing: mkdir -p "$HOME/something" – Lorenz Keel May 25 '21 at 13:39

1 Answers1

4

Inside single or double quotes, ~ is managed literally as a tilde character, it is not translated to "current-user-home-folder".

You have two alternative ways to achieve your target:

  • using double quotes and replacing the tilde symbol with the variable $HOME. Summarizing: mkdir -p "$HOME/something". The $, opposite to ~, is not treated as a literal inside double quotes.
  • just not quoting the tilde. Summarizing: mkdir -p ~/'something'
Lorenz Keel
  • 8,905