Backslashes on the way: do they influence anything?

Asked

Viewed 89 times

6

I never understood why some systems use the \ and others use the / for the same purpose: to divide paths and addresses. There is no pattern from which to use, or at least couldn’t find a.

While programming and defining paths, which of these bars should we use?

I have an example that, in Laravel, when calling the function:

$caminho = storage_path();
$caminhoConcatenado = $caminho . "/informacao/concatenada.txt";

In Windows, I have the way:

$caminho = "C:\laragon\www\foo\bar\storage"

And in Centos I have:

$caminho = "/home/admin/web/quick_fox/lazy_dog/"

When I provide information to $caminho, i don’t know which of the bars to use. If I use a /, in Centos will be correct and readable, but in Windows, I will have approximately this:

$caminhoConcatenado = "C:\laragon\www\foo\bar\storage/informacao/concatenada.txt"

In these situations,

  • the path is understood correctly?
  • is correct to perform this type of operation?
  • if it’s not right, what should I do?

2 answers

6

Just complementing the friend’s reply with a curiosity, since you said you never understood why some systems use the bar \ and others use the / for the same purpose.

Well, windows is the only one that uses \, Unix systems and Urls use /, This occurs in the early days of Microsoft at the time of MS-DOS 1.0.

Many of the MS-DOS 1.0 utilities were written by IBM, and they used the / as Character switch (a character that is part of a command). For example in the command dir /? we use /? to display the command help list dir (in Unix systems the Character switch is the -).
At the time the IBM Character switch / conflicting with the directory separator of another S.O. didn’t matter so much to developers because DOS didn’t work with directories, it was just files in a root directory.

In MS-DOS 2.0 they implemented the concept of hierarchy, but could not use the same Unix standard / as this was already used as switch Character, also could not use the . because this was used to separate the name of the files from the extentions, so the solution was to use \.

And that’s why microsoft uses \.
We can check this out on the blog of Larry Osterman of Microsoft itself.

5


It depends a little on the language, but in general it should not deal with paths on their own, the languages have their own library that knows how to handle it and you work the way without worrying about this detail that is of the operating system. In some cases if the language does not have a standard library it is possible to use one that handles it.

Most people do wrong or use a language that does not provide something ready and correct, which is still a mistake of programming, then one has two options:

  • do in a way and establish that the code on the wheel on the platform that the code was thought
  • take care at hand to deal with format differences with a huge job and potential to do wrong

Some languages have a constant with the separator, which helps somewhat generalize the code, but doesn’t make it completely simple to do it right. Example in PHP.

It depends on where you use the text presented in the question. There it is only a text, for the system is not a path, only you reading thinks it is. It is true that Apis that access files in the operating system usually understand this text, so can have an API that understands it perfectly and have API that does not understand, including because it has API that does not read only the path, reads other things and the other bar is used to identify something else and is valid in the text, there is ambiguous. Not knowing which API you are using has no way of stating whether it works or not. This feature is not of the language itself, at most of what accompanies it.

In PHP it generally accepts anyway, but I will not guarantee that it always happens. If any way the general recommendation for PHP and other languages is to use a standard, it is usually more appropriate to use the Unix standard. If any API doesn’t work well even in Windows in general we consider it a bad API and you will have to turn around.

If you want to know about the use of characters already have an answer about this in Why in file and folder names, some characters are not yet accepted?.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.