Why does backslash work for file or folder names, but question mark less so

filesystemfinderhtml

I created a HTML file as this name on Sierra 10.12.6: test\55.html and it showed up in the address bar of Firefox as:

Users/MYNAME/Desktop/test\55.html

I then made a duplicate as test?\55.html and it rendered in the browser as:

Users/MYNAME/Desktop/test%3F\55.html

If naming files, why does backslash seem to work? I know forward slash (/) doesn't and renders as :: in Finder, but for backslash and question mark, why do these work in Finder and should they be avoided in filenames?

I'm on HFS, not APFS, so does this relate to it, and in Mojave, would the problems around these not exist?

If anyone can answer I'd be grateful for this; done some basic research but am not fully sure.

Best Answer

Characters to avoid in filenames

You shouldn't be using slashes or question marks in your filenames. This can cause issues depending on the operating system/file system being used.

There's already an excellent answer in SuperUser What characters are safe in cross-platform file names for Linux, Windows and OS-X addressing this, but to summarize:

  • forward slashes are used to separate values in the path in *nix operating systems
  • back slashes are used to escape spaces in file names in *nix operating systems and as path separators in Windows.
  • Windows does not like \/:*?"<>| in paths or filenames.

As for the "double colon" (::) this is a convention used by Finder to represent the home directory. The single colon is a path separator.

Question Marks (and other characters) in URLs

This is not a good idea. First off, Windows users (the browsers actually) are going to have a hard time with the the file with the question mark. Secondly, The question mark indicates a query string in PHP meaning, you're sending the PHP language a query or a command to execute.

For instance, take a look at this URL:

https://www.bing.com/search?form=MOZLBR&pc=MOZI&q=php+url+question+mark

Putting an arbitrary question mark in the file name of the URL will definitely throw a monkey in the wrench.

Additionally, you're not "escaping" characters in the URL with the percent sign - it's called "Percent Encoding or URL Encoding." This allows you to use special or reserved characters (like a space) in the URI and have it be functional.