5 ms·
It tells the shell utility that any remaining arguments are not options, but instead files or whatever the script might process. You know, in case someone make
by less_less 2y ago
It tells the shell utility that any remaining arguments are not options, but instead files or whatever the script might process. You know, in case someone makes a file called -rf.
- pletnes 2y agoBut not all shell utilities follow this particular convention
- ribcage 2y agoYes. The famous echo on Linux systems does not have it and therefore it's impossible to print the string "-n o p e", because -n will be interpreted as an option.
- bonzini 2y agoecho is not portable anyway, use "printf %s STRING" or "printf '%s\n' STRING".
- ribcage 2y agoYes, that's what I use. Sometimes I still get tempted to use echo because there's less typing...
- ezequiel-garzon 2y agoIt does if single or double quotes are used, right? Which would be necessary (or preferred to multiple backslashes) quite often.
- bonoboTP 2y agoNo, the quotes are not seen by the program. The program receives a list of strings, it does not get the information about whether and how those strings were originally quoted in the shell. Programs can also be directly called with lists of strings as in execve, so often it does not even make sense to ask if the arguments were quoted or not. Quotes live on a different level of abstraction.
- danadam 2y ago> No, the quotes are not seen by the program. The program receives a list of strings, it does not get the information about whether and how those strings were originally quoted in the shell. With quotes the program will receive a single argument -n␣o␣p␣e instead of multiple ones -n, o, p, e. At least it works on the machine here: ]$ echo "-n o p e" -n o p e ]$ /bin/echo "-n o p e" -n o p e
- bonoboTP 2y agoYes, I think there was some misremembering here. The nontrivial thing is to print out -n itself with echo. For example, echo doesn't treat "--" specially, so "echo -- -n" prints "-- -n".
- account42 2y agoNote that this is true for POSIX sytems but not e.g. for Windows. There the program receives the command-line as-is and is responsible for parsing it into an array. There are two different standard functions to do this parsing for you (with slightly different quoting behavior) but you could also create your own that requires options to not be quoted.
- hackerthemonkey 2y agoYes. It’s more of a convention. If a shell utility isn’t following it then it won’t mean what we think it would mean. Also - it helps a lot when a utility accepts a path that may or may not contain hyphens.
- ndsipa_pomu 2y agoMost of the commonly used ones do, so it's easiest to just always do it and then remember the two or three utils that don't like it.