简体   繁体   中英

why cant i use & in python command line

I am trying to receive using python command line arguments but i have some problems in receiving & along with arguments

script

import sys
START_D = sys.argv[1]
print START_D

This works fine

python test.py sdsss
sdsss

But if i use & it is giving some random stuffs

python test.py sds&ss
[1] 8682
sds
Netid  State      Recv-Q Send-Q Local Address:Port                 Peer Address:Port                
u_seq  ESTAB      0      0      @0001b 25095                 * 25096                
u_seq  ESTAB      0      0      @0001a 25086                 * 25087                
u_seq  ESTAB      0      0      @00020 74036                
.
.
.
.
.
tcp    ESTAB      0      0      2405:205:830d:2a38:492a:5a32:cffe:eca1:34196                  2404:6800:4009:807::2003:https                
[1]+  Done                    python test.py sds

What was the issuse how can i fix it and i want to use & in parameters so how can i use it along when i send parameters?

& is a metacharacter in your shell , and tells your shell to put the command in the background. This is why you see the PID of the backgrounded process echoed on your terminal:

[1] 8682

Escape it by using a backslash or quoting if you don't want the shell to interpret it:

$ python test.py sds\&ss
$ python test.py "sds&ss"

The sign & has a meaning in terminal. It means execute the first command in the background AND the second one in the front. Unfortunately ss is a valid command as well. You execute python test.py sds in the background and execute ss . ss is a network monitor protocol, that is why you see in those outputs.

Use your command like this: python test.py 'sds&ss'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM