Button to go to Home Page Home Button to go to About Page About Button to go to Archive Page Archive

Working with directories that start with a - character

03/21/2017 • 2 minute read

If you ever have the misfortune of working with an application that starts its directories with a - character, then you will know the pain of pretty much every standard bash tool breaking as they all expect - to be used for flags like so:

$ cd -test
bash: cd: -t: invalid option cd: usage: cd [-L|[-P [-e]] [-@]] [dir]

Intuitively, you would think that escaping the string with -test would work, but that produces the same error.

To solve this, I had to dive into some POSIX standards and discovered that the following is how you safely discard the first argument and not trigger Bash into thinking that -test is your flag:

$ cd -- -test

Easy enough once you know that trick. You can also cheat and define the path explicitly, but that isn’t as fun to whip out at parties:

$ cd ./-test

Got Questions, Comments, or Insults?

You May Also Enjoy