I have a program that reads a natural language sentence from command line and does something with it.
Some punctuation marks are not accepted: either I get a specific error message (like when I use parenthesis), or I get >
(for example when I type '
), as if the terminal is still waiting for something.
A quick fix is to include the single "problematic" words in quotation marks, but it doesn't seem like a great fix to me.
Isn't there a way to prepare the command line to take any character as a valid input, without having the 'user' to think about what to include in quotation marks or not?
For the sake of completeness, that's how I coded the command line thing: int main(int argc , char ** argv){
, I guess it's the standard way.
I'm using the last Ubuntu, don't know if that's relevant.
The problem here is less to do with your program and more to do with what is invoking it.
When invoking your program from a shell (like bash
), the shell will try to do you a favor and split the command line arguments on whitespace. It'll also interpret special characters like >
, ;
and (
for redirection, splitting commands, and starting subshells.
In order to turn this behavior off, these special characters must be escaped by preceding them with a \
or by enclosing command line arguments in single- or (only in most cases) double-quotes.
This is only a requirement if your program is executed by a shell. If it's executed by a program you control, you will have direct control over the list of arguments passed to your program (the argv
in your program's main()
function).