> On Mon, 13 Apr 2015 06:41:10 +0000, Kaz Kylheku wrote:
>
>> for example, let's try your printf arguments:
>>
>> $ for x in -- "-n \\fds \\\n" ; do printf "<%s>\n" "$x" ; done <-->
>> <-n \fds \\n>
>>
>> So, aha, what goes into printf is -- and -n \fds \\n
>
> So, in this case, the actual command processed by printf innerly is this:
If printf is a program in /usr/bin rather than a shell builtin, it's quite externly. :)
> printf -- -n \fds \\n
>
> The -- told printf not treat anything as args/options after this symbol.
Yes. This is not explicitly required by POSIX, by the way. POSIX says that printf
takes no options.
However, even if a command takes no options, the requirement to support -- applies,
because:
The first -- argument that is not an option-argument should be accepted as a
delimiter indicating the end of options. Any following arguments should be
treated as operands, even if they begin with the '-' character.
For a command which takes no options, -- is a "non-option argument".
Actual implementations of printf have options. Bash builtin:
$ printf --version
bash: printf: --: invalid option
printf: usage: printf [-v var] format [arguments]
Coreutils printf:
$ /usr/bin/printf --version
printf (GNU coreutils) 8.13
[ ... etc ]
> And so, I analysis it as follows:
>
> -n goes out as -n;
> \f is a non-printable control character -- formfeed -- and will be
> garbled or nothing in the output;
If the output is a terminal, it can actually do something, like clear
the screen. On a line printer, it can eject the page.
> ds will goes out as ds;
> \\ once again, the first \ will escape the second one, and goes out a
> literal \;
> n goes out as n.
>
> Finally, I got the following things:
>
> -n <formfeed>ds \n
>
> Am I right?
That's it. Basically, very similar to what the C printf would do.
> Furthermore, I do the following testings on echo:
>
> werner@debian:~$ echo "-n"
> werner@debian:~$ echo "-n "
> -n
> werner@debian:~$ echo "-n \\fds \\\n"
> -n \fds \\n
>
> As you can see, the last two commands work, but the first one does
> nothing. Why?
Probably because the echo command has fallback logic for any unrecognized
option character.
Try
echo -ne
(e is recognized by your echo) versus
echo -nZ
(Z is not recognized; and so the behavior of -n is suppressed too and the
entire argument is just printed.)
The "-n " argument is proably being regarded by your echo as the options -n and
-<space> specified together as a clump.
None of this is specified in the standard however. I think that your implementors
either don't care about standard conformance, or are interpreting the standard
as meaning that if -n is present in the first argument even as a clump,
the behavior is implementation-defined.
(My reading is that the specification only concerns itself whether or not -n is
literally the first argument, not whether or not it is a member of a clump like
-ne or -nZ; but that interpretation forbids supporting additional options like -e.)