Search code examples
pythonargumentscentosampersand

error sending an argument with an ampersand character in the value in python?


I'm dealing 2 scripts in python. the first one needs to send a value or an argument to the second script. Now the problem is whenever I send the value to the 2nd script, the 2nd script couldn't get all the arguments that I sent. the value that Im sending is a URL and it contains an ampersand. I noticed that it kept on cutting the value to the first appearance of &.

lets sat for example, I need to pass this :

http://www.google.com/jfljflfjej&12345

the 2nd script will receive only this :

http://www.google.com/jfljflfjej

what do I need to do to be able to catch the correct value? And what other characters that have the same issue as this?


Solution

  • You need to put quotes around the whole value, including the ampersand, as it is a special shell character. Alternatively, you can escape just the ampersand by putting a backslash in front of it:

    http://www.google.com/jfljflfjej\&12345
    

    The ampersand signals to the shell you want to put the command up to that point in background mode.

    Any of the following characters are special in a shell:

     \ ' " ` < > | ; <Space> <Tab> <Newline> ( ) [ ] ? # $ ^ & * =
    

    These need to be escaped in the same way; use a backslash or put quotes around the value.