You're viewing a comment by Mikael Auno and its responses.

Mikael Auno Permalink
July 12, 2012, 12:42

You're explanation as to why echo does not interpret -n as a flag/an option is incorrect.

Notice that "$x$y" were quoted. If we didn't quote it, echo would interpret the $x$y as regular arguments, and would first try to parse them to see if they contain command line switches. So if $x contains something beginning with -, it would be a command line argument rather than an argument to echo:

The quoting only ensures that -n foo is treated as one string and not split on the space. echo will still see the - and test whether it is a valid flag/options. It just happens to be that "n foo" is not a valid flag/option to echo, and so it is treated as a regular string.

The quoting is only known to Bash, which is in charge of parsing the command line into strings and doing variable substitution. Bash (generally, echo just happens to be a built in function in bash) knows nothing about what is regular argument to a command and what is a flag/an option.

Hopefully, the following examples will illustrate the differences:

$ ls
bar  bas  foo

$ ls --reverse
foo  bas  bar

$ ls "--reverse"
foo  bas  bar

$ ls --rev erse 
ls: cannot access erse: No such file or directory

$ ls "--rev erse"
ls: unrecognized option '--rev erse'
Try `ls --help' for more information.

Reply To This Comment

(why do I need your e-mail?)

(Your twitter name, if you have one. (I'm @pkrumins, btw.))

Type the first letter of your name: (just to make sure you're a human)

Please preview the comment before submitting to make sure it's OK.