Alfred M. Szmidt wrote: > Here is a fun one, how does one output `-n' (literal string) (or any > other option that echo accepts) using echo?
Ironically 'echo' is one of the most troublesome commands for portable usage. You would be much better off using 'printf'. $ printf -- "-n\n" -n $ printf "%s\n" -n -n > $ /bin/echo -- -n > -- -n POSIX requires that 'echo' not recognize "--" as an end of options argument and requires that it be recognized as a string. This is for compatibility with traditional implementations. http://www.opengroup.org/onlinepubs/009695399/utilities/echo.html > $ /bin/echo - "-n" > - -n Right. Nothing special there. Just strings. > $ /bin/echo '-n' The first argument is "-n". POSIX says: If the first operand is -n, or if any of the operands contain a backslash ( '\' ) character, the results are implementation-defined. GNU echo defines them with the BSD semantics of not outputing a newline. > $ /bin/echo "-n" Of course you know the shell handles argument quoting. The command never receives the single or double quotes. So "-n" and '-n' are identical in resulting behavior. You could also use 'cat'. A venerable old portable echo-like method is to use cat with a here-document. $ cat <<EOF > -n > EOF -n That is probably better than other shenanigans like: $ echo ' -n' | sed 's/ //' -n Bob
