News for hackers. Stuff that matters.
Re:Use Long Options in Scripts by pletnes on Saturday March 22, 2025 @05:15AM (#43444489)
But not all shell utilities follow this particular convention
Re:Use Long Options in Scripts by hackerthemonkey on Saturday March 22, 2025 @06:14AM (#43444701)
Yes. 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.

Re:Use Long Options in Scripts by ndsipa_pomu on Saturday March 22, 2025 @06:31AM (#43444793)
Most 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.
Re:Use Long Options in Scripts by ribcage on Saturday March 22, 2025 @05:47AM (#43444591)
Yes. 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.
Re:Use Long Options in Scripts by ezequiel-garzon on Saturday March 22, 2025 @08:06AM (#43445187)
It does if single or double quotes are used, right? Which would be necessary (or preferred to multiple backslashes) quite often.
Re:Use Long Options in Scripts by bonoboTP on Saturday March 22, 2025 @09:38AM (#43445578)
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. 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.

Re:Use Long Options in Scripts by danadam on Saturday March 22, 2025 @10:41AM (#43445947)
> 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
Re:Use Long Options in Scripts by bonoboTP on Monday March 24, 2025 @03:27PM (#43464554)
Yes, 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".
Re:Use Long Options in Scripts by account42 on Monday March 24, 2025 @08:19AM (#43460127)
Note 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.
Re:Use Long Options in Scripts by bonzini on Saturday March 22, 2025 @06:30AM (#43444787)
echo is not portable anyway, use "printf %s STRING" or "printf '%s\n' STRING".
Re:Use Long Options in Scripts by ribcage on Saturday March 22, 2025 @06:55AM (#43444894)
Yes, that's what I use. Sometimes I still get tempted to use echo because there's less typing...