4 ms·
I too often avoid spaces in filenames, but when writing scripts it's far better to ensure that you cope with them properly. The trick is to always quote your va
by ndsipa_pomu 3y ago
I too often avoid spaces in filenames, but when writing scripts it's far better to ensure that you cope with them properly. The trick is to always quote your variables by default and then run shellcheck on your script so that it can flag up the corner cases where quoting acts to unquote the string. Of course, it'll also flag up a host of other possible issues which are well worth fixing or at least understanding why it's a possible issue.
Another common footgun is not including a double dash ('--') at the end of command options so that the following filename won't be interpreted as an option when it begins with a dash.
- vram22 3y ago>Another common footgun is not including a double dash ('--') at the end of command options so that the following filename won't be interpreted as an option when it begins with a dash. Yes. And if you have a file name that begins with a dash, and you want to delete it, you can use this command: $ rm -- -filename where -filename is that file. Here, the -- (double dash) means "end of the command-line options", so any argument after that is treated as a non-option argument, in this case, a filename for rm to delete. This -- (double dash) works with many Unix commands. It was introduced relatively early on in Unix history, after the issue of filenames starting with a dash was found. Although it is more likely that you would want to rename the file, to remove the dash from the name.
- ndsipa_pomu 3y agoAnother way of working around the issue of filenames beginning with a dash is to just put "./" in front of it. That's not always useful in scripts though if you don't know whether the variable will be a relative or absolute filename (though you could check for that easily enough).
- throwaway858 3y agoIf you ever need to use GNU make (still a popular tool) then you will be glad that you kept a policy of avoiding spaces in file names. There are places in Makefiles where no amount of quoting will help you, and it simply cannot handle files with spaces. Yeah, this is a problem with the tool that should be fixed, but my understanding is that it will never be fixed because of architectural reasons. And it's a very popular, useful, and most importantly, ubiquitous tool. So this is another good reason to simply have a policy of banning filenames with spaces (and there are many more reasons).