21

In Bash, how could I create a directory named -p?

mkdir -p failed.
mkdir "-p" failed.
mkdir "\-p" failed.
mkdir \-p failed.

Oliver Salzburg
  • 89,072
  • 65
  • 269
  • 311
Jichao
  • 7,940

2 Answers2

39

Most utilities (all POSIX compliant ones except for test and echo) support an "end of options" option --, so you could run:

mkdir -- -p

This is especially useful when renaming or removing files that could potentially start with a dash. In scripts you should always use

mv -- "$filename"

instead of a plain mv "$filename" or, even worse, an unquoted filename.

slhck
  • 235,242
17

mkdir ./-p

Keep in mind that most other programs would need to use the same "trick".

user219095
  • 65,551